我有以下 XML 文档
<a:rootElement xmlns:a="http://a/1" xmlns:b="http://b/1" xmlns:c="http://c/1">
<child1 type="b:type"/>
<child2 type="c:type"/>
</a:rootElement>
现在我想更改命名空间的 URI,所以我得到以下结果
<a:rootElement xmlns:a="http://a/2" xmlns:b="http://b/2" xmlns:c="http://c/2">
<child1 type="b:type"/>
<child2 type="c:type"/>
</a:rootElement>
其他什么都不应该改变。我使用以下样式表进行了尝试。
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:a="http://a/2"
xmlns:b="http://b/2"
xmlns:c="http://c/2" >
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select="document('')/*/namespace::*[name()='a']"/>
<xsl:copy-of select="document('')/*/namespace::*[name()='b']"/>
<xsl:copy-of select="document('')/*/namespace::*[name()='c']"/>
<xsl:copy-of select="node()|@*"/>
</xsl:element>
</xsl:template>
我得到以下错误输出。
<a_0:rootElement xmlns:a_0="http://a/1" xmlns:a="http://a/2" xmlns:b="http://b/2" xmlns:c="http://c/2">
<child1 type="b:type" xmlns:a="http://a/1" xmlns:b="http://b/1" xmlns:c="http://c/1"/>
<child2 type="c:type" xmlns:a="http://a/1" xmlns:b="http://b/1" xmlns:c="http://c/1"/>
</a_0:rootElement>
我也尝试了其他一些方法,但也没有想要的输出。甚至可以使用 XSLT 以这种方式更改 namespace 吗?
感谢您的任何建议
最佳答案
本次改造 :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a1="http://a/1"
xmlns:b1="http://b/1"
xmlns:c1="http://c/1"
xmlns:a="http://a/2"
xmlns:b="http://b/2"
xmlns:c="http://c/2"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vDoc" select="document('')/*"/>
<xsl:variable name="vnsA" select="$vDoc/namespace::*[name()='a1']"/>
<xsl:variable name="vnsB" select="$vDoc/namespace::*[name()='b1']"/>
<xsl:variable name="vnsC" select="$vDoc/namespace::*[name()='c1']"/>
<xsl:variable name="vnsA2" select="$vDoc/namespace::*[name()='a']"/>
<xsl:variable name="vnsB2" select="$vDoc/namespace::*[name()='b']"/>
<xsl:variable name="vnsC2" select="$vDoc/namespace::*[name()='c']"/>
<xsl:template match="*">
<xsl:variable name="vNS" select="namespace-uri()"/>
<xsl:variable name="vnewNS">
<xsl:choose>
<xsl:when test="$vNS = $vnsA">
<xsl:value-of select="$vnsA2"/>
</xsl:when>
<xsl:when test="$vNS = $vnsB">
<xsl:value-of select="$vnsB2"/>
</xsl:when>
<xsl:when test="$vNS = $vnsC">
<xsl:value-of select="$vnsC2"/>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:element name="{name()}" namespace="{$vnewNS}">
<xsl:copy-of select=
"namespace::*[not(contains('|a|b|c|', concat('|', name(), '|')))]
"/>
<xsl:copy-of select="namespace::*[name() = 'a' and not(. = $vnsA)]"/>
<xsl:copy-of select="namespace::*[name() = 'b' and not(. = $vnsB)]"/>
<xsl:copy-of select="namespace::*[name() = 'c' and not(. = $vnsC)]"/>
<xsl:if test="namespace::*[name() = 'a' and . = $vnsA]">
<xsl:copy-of select="$vnsA2"/>
</xsl:if>
<xsl:if test="namespace::*[name() = 'b' and . = $vnsB]">
<xsl:copy-of select="$vnsB2"/>
</xsl:if>
<xsl:if test="namespace::*[name() = 'c' and . = $vnsC]">
<xsl:copy-of select="$vnsC2"/>
</xsl:if>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
当应用于提供的 XML 文档时 :
<a:rootElement
xmlns:a="http://a/1"
xmlns:b="http://b/1"
xmlns:c="http://c/1">
<child1 type="b:type"/>
<child2 type="c:type"/>
</a:rootElement>
产生想要的、正确的结果 :
<a:rootElement xmlns:a="http://a/2" xmlns:b="http://b/2" xmlns:c="http://c/2">
<child1 type="b:type"/>
<child2 type="c:type"/>
</a:rootElement>
关于xslt - 在根元素上用 XSLT 替换多个命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8896271/