【发布时间】:2026-02-03 05:10:02
【问题描述】:
我尝试使用 XSLT 更改标签/属性命名空间。
来自输入 XML:
<office:document-meta
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0">
<office:meta>
<meta:user-defined meta:name="Info 1">in</meta:user-defined>
</office:meta>
</office:document-meta>
我想要输出 XML:
<office:document
xmlns:office="http://openoffice.org/2000/office"
xmlns:meta="http://openoffice.org/2000/meta">
<office:meta>
<meta:user-defined meta:name="Info 1">in</meta:user-defined>
</office:meta>
</office:document>
我的 XSLT 正在正确更改命名空间,但属性命名空间除外:
<xsl:stylesheet
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="xml" indent="yes"/>
<!-- Match root and output in new namespace -->
<xsl:template match="office:document-meta/office:meta" >
<office:document
xmlns:office="http://openoffice.org/2000/office"
xmlns:meta="http://openoffice.org/2000/meta" >
<office:meta>
<xsl:apply-templates select="@*|node()"/>
</office:meta>
</office:document>
</xsl:template>
<!--
Expected <meta:user-defined meta:name="Info 1">in</meta:user-defined>
Received <meta:user-defined xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" meta:name="Info 1">in</meta:user-defined>
-->
<xsl:template match="meta:user-defined">
<xsl:copy copy-namespaces="no" >
<xsl:attribute name="meta:name"><xsl:value-of select="@*"/></xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
使用此 XSL 接收:
<office:document
xmlns:office="http://openoffice.org/2000/office"
xmlns:meta="http://openoffice.org/2000/meta">
<office:meta>
<meta:user-defined
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
meta:name="Info 1">in</meta:user-defined>
</office:meta>
</office:document>
如何告诉 Saxon 9.3.0.5 摆脱属性 meta:name 的旧属性命名空间?
如何删除 xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" ?
如果有人能帮忙提前谢谢!
【问题讨论】:
-
您的样式表不会产生您声称的结果。
标签: xml xslt namespaces saxon