xslt - 在根元素上用 XSLT 替换多个命名空间

标签 xslt namespaces

我有以下 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/

相关文章:

重构期间的 PHP 命名空间最佳实践?

c++ - 在类中定义的友元函数的完全限定名称是什么?

xslt - xsl :sort by variable

xslt - 已设置属性的 Concat 参数

xml - 使用参数值,如 xslt 中的 select

xml - 使用一个 XSLT 生成 XML 和 XSD 文档

python - 使用装饰器将函数插入模块的命名空间

xml - 使用 SSIS 加载 XML 对 SQL 具有多个命名空间

c++ - 什么是 std::map<K,V>::map;以及如何知道在实现/使用 STL 容器和函数时使用什么命名空间?

xml - XSL-FO:在每个 <xi:include> 之间强制分页