【问题标题】:Changing a namespace value in an XSL transformation?在 XSL 转换中更改命名空间值?
【发布时间】:2009-10-22 07:02:34
【问题描述】:

我不确定这是否可能,因为我对 XSLT 和其他东西很陌生,但也许你们中的一些人可以在这里帮助我?这有点棘手,我在互联网上没有找到类似的东西:

问题是我有一个声明了命名空间的输入 xml,我只需要对其进行轻微更改(添加或删除属性,或将它们转移到其他位置)。但同时,我必须更新文档的文档标签中的命名空间引用。因此,例如,输入 xml 可能看起来像这样:

<order
  xmlns="some.url.01"
  xmlns:ns2="some.other.url"
  xmlns:ns3="another.one"
>
  <orderEntry>
    <orderControl>
      <mandant>test</mandant>
      <businessUnit>test</businessUnit>
      <inboundChannel>test</inboundChannel>
      <timestamp>timestamp</timestamp>
      <requestedDocuments>
        <ns2:document>orderForm</ns2:document>
      </requestedDocuments>
    </orderControl>
  </orderEntry>
</order>

生成的 xml 应如下所示:

<order
  xmlns="some.url.02"
  xmlns:ns2="some.other.url.02"
  xmlns:ns3="another.one.02"
>
  <orderEntry>
    <orderControl>
      <mandant>test</mandant>
      <businessUnit>test</businessUnit>
      <inboundChannel>test</inboundChannel>
      <!-- deleted timestamp for example -->
      <requestedDocuments>
        <ns2:document>orderForm</ns2:document>
      </requestedDocuments>
    </orderControl>
  </orderEntry>
</order>

但我唯一得到的是:

<order
  xmlns="some.url.02"
>
  <orderEntry>
    <orderControl>
      <mandant>test</mandant>
      <businessUnit>test</businessUnit>
      <inboundChannel>test</inboundChannel>
      <!-- deleted timestamp for example -->
      <requestedDocuments>
        <ns2:document xmlns:ns2="some.other.url.02">orderForm</ns2:document>
      </requestedDocuments>
    </orderControl>
  </orderEntry>
</order>

现在,对于你们中的一两个人来说,这可能没什么大不了的,但我有一个限制,即输出文档应该与输入文档一一对应,除了请求的更改(命名空间更改和删除)。

我的 XSLT 看起来像这样:

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="some.url.02"
  xmlns:ns2="some.other.url.02"
  xmlns:ns3="another.one.02"
>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>

  <xsl:strip-space elements="*"/>

  <xsl:template match="*">
    <xsl:choose>
      <xsl:when test="name(.) != 'timestamp'">
        <xsl:element name="{node-name(.)}">
          <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:attribute name="{node-name(.)}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

有人可以帮忙吗?命名空间很棘手:(

P.S.:谁编辑了我的条目:谢谢 :)

【问题讨论】:

    标签: xslt xml-namespaces


    【解决方案1】:

    您可以使用命名空间属性在输出元素上设置命名空间:

    <xsl:element name="{node-name(.)}" namespace="http://www.bar.org">
      // ...
    </xsl:element>
    

    请注意,命名空间必须是 URI,尽管我希望您知道这一点,但在您的示例中使用 URI 可能是个好主意。

    这里是优秀的 ZVON 教程的链接,该教程提供了一些示例: http://www.zvon.org/xxl/XSLTreference/Output/xslt_element_namespace.html

    我同意命名空间很棘手。如您所知,前缀在语义上无关紧要,但许多系统允许您出于审美原因选择前缀。也看看撒克逊 (http://saxon.sourceforge.net/)

    编辑我想你会在这里找到答案: XSLT root tag namespace instead of element attribute namespace

    【讨论】:

    • 您可以将 URI 存储在 &lt;xsl:variable/&gt; 中,并像 namespace="{$uri}" 一样使用它。如果您更频繁地需要它,那就太好了。
    • 如果我也比这正确,也会产生相同的 XML,标签中的命名空间使用这些命名空间,对吧?
    • 感谢千千万万!该链接确实对我有所帮助,现在它可以工作了,耶! =)
    【解决方案2】:
    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:ns1_src="some.url.01"
      xmlns:ns2_src="some.other.url"
      xmlns:ns3_src="another.one"
      xmlns="some.url.02"
      xmlns:ns2="some.other.url.02"
      xmlns:ns3="another.one.02"
    >
      <!-- 
        Note that all the source namespaces got their own new "*_src" prefix. 
        The target namespaces take over the original prefixes. 
        "some.url.02" is the new global namespace.
      -->
    
      <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <!-- the identity template to copy everything, unless 
           it has been declared otherwise -->
      <xsl:template match="node() | @*">
        <xsl:copy>
          <xsl:apply-templates select="node() | @*" />
        </xsl:copy>
      </xsl:template>
    
      <!-- three templates to handle elements -->
      <xsl:template match="ns1_src:*">
        <xsl:element name="{local-name()}">
          <xsl:apply-templates select="node() | @*" />
        </xsl:element>
      </xsl:template>
    
      <xsl:template match="ns2_src:*">
        <xsl:element name="ns2:{local-name()}">
          <xsl:apply-templates select="node() | @*" />
        </xsl:element>
      </xsl:template>
    
      <xsl:template match="ns3_src:*">
        <xsl:element name="ns3:{local-name()}">
          <xsl:apply-templates select="node() | @*" />
        </xsl:element>
      </xsl:template>
    
      <!-- three templates to handle attributes -->
      <xsl:template match="@ns1_src:*">
        <xsl:attribute name="{local-name()}">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:template>
    
      <xsl:template match="@ns2_src:*">
        <xsl:attribute name="ns2:{local-name()}">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:template>
    
      <xsl:template match="@ns3_src:*">
        <xsl:attribute name="ns3:{local-name()}">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:template>
    
      <!-- timestamps will be ignored -->
      <xsl:template match="ns1_src:timestamp" />
    
    </xsl:stylesheet>
    

    输出:

    <order xmlns="some.url.02">
      <orderEntry>
        <orderControl>
          <mandant>test</mandant>
          <businessUnit>test</businessUnit>
          <inboundChannel>test</inboundChannel>
          <requestedDocuments>
            <ns2:document xmlns:ns2="some.other.url.02">orderForm</ns2:document>
          </requestedDocuments>
        </orderControl>
      </orderEntry>
    </order>
    

    【讨论】:

    • 感谢您的帮助,但我认为我解释得不够清楚,所以再试一次:您给我的结果输出就是我得到的,但我想要的是 xmlns: ns2 声明在 order 标记中。 =/
    • @Wickermoon - 我的答案中的新链接可能会对您有所帮助
    【解决方案3】:
    <xsl:template match="a:*">
      <xsl:element name="{local-name()}"
                   namespace="http://example.com/B">
        <xsl:copy-of select="@*" />
        <xsl:apply-templates />
      </xsl:element>
    </xsl:template>
    

    它在命名空间中搜索前缀为a 的任何元素,并将其替换为与命名空间http://example.com/B 同名的元素。所有属性都“按原样”复制,然后评估所有子项。

    根据需要在其中或周围添加您的自定义处理。

    【讨论】:

    • @Boldewyn - 你不需要使用属性值模板 {...} 作为名称吗?
    • 不幸的是,当我将它添加到我的 XSLT 时,它会创建与以前完全相同的 xml,在每个元素中都有具有该名称空间的名称空间,而不是在根元素中声明一次名称空间(顺序在这种情况下)。 =(
    • @peter.murray.rust:哦,是的,你是对的。抱歉,Wickermoon,我会在稍后更新我的答案。
    • @Wickermoon:好的,现在它做了它宣传的事情。将命名空间 A 的元素作为命名空间 B 的输入输出同义元素。
    • 顺便说一句:这次我在 Saxon 9.something 中测试了它。
    【解决方案4】:

    您是否使用 Ant 的 XSLT 任务进行转换?

    如果答案是肯定的,您可能希望从 Sun JDK 1.5+ 附带的默认 XSLT 引擎切换。阅读this

    另外,请阅读 this article 关于 XSLT 中的命名空间

    【讨论】:

    • 感谢您的链接,但不,我实际上使用 java 和 Saxon9.something 进行转换。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    相关资源
    最近更新 更多