【问题标题】:Changing namespace using XSLT - Not copying all properties of the XML使用 XSLT 更改命名空间 - 不复制 XML 的所有属性
【发布时间】:2014-05-18 22:08:36
【问题描述】:

我有一个场景,我必须将 XML 文档中的命名空间从一组更改为另一组。我可以成功更改命名空间,但字段的属性没有被复制,比如属性。我基本上想复制整个 XML,只需更改命名空间。

例如,XML

<ns1:Response xmlns:ns2="http://www.w3.org/TR/html4/2" xmlns:ns3="http://www.w3.org/TR/html4/3" xmlns:ns4="http://www.w3.org/TR/html4/4" xmlns:ns5="http://www.w3schools.com/furniture" xmlns:myns1="http://www.w3.org/TR/html4/1" xmlns:myns2="http://www.w3.org/TR/html4/2" xmlns:myns3="http://www.w3.org/TR/html4/3" xmlns:myns4="http://www.w3.org/TR/html4/4" xmlns:myns5="http://www.w3schools.com/furniture" xmlns:ns1="http://www.w3.org/TR/html4/1">
   <ns2:task attribute="one">..</ns2:task>
   <ns2:address>..</ns2:address>
   <ns2:pin>..</ns2:pin>
   <ns3:address>
      <ns4:add1 attribute="2">..</ns4:add1>
      <ns4:add2>..</ns4:add2>
      <ns4:add3>
        <ns5:asdf>..</ns5:asdf>
        <ns5:qwe>..</ns5:qwe>
      </ns4:add3>
      <ns4:add4>..</ns4:add4>
    </ns3:address>
    <ns2:query>..</ns2:query>
</ns1:Response>

转换成

<?xml version="1.0" encoding="UTF-8"?>
<myns1:taskListResponse xmlns:ns2="http://www.w3.org/TR/html4/2" xmlns:ns3="http://www.w3.org/TR/html4/3" xmlns:ns4="http://www.w3.org/TR/html4/4" xmlns:ns5="http://www.w3schools.com/furniture" xmlns:myns1="http://www.w3.org/TR/html4/1" xmlns:myns2="http://www.w3.org/TR/html4/2" xmlns:myns3="http://www.w3.org/TR/html4/3" xmlns:myns4="http://www.w3.org/TR/html4/4" xmlns:myns5="http://www.w3schools.com/furniture" xmlns:ns1="http://www.w3.org/TR/html4/1">
       <myns2:task attribute="one">..</myns2:task>
       <myns2:address>..</myns2:address>
       <myns2:pin>..</myns2:pin>
       <myns3:address>
          <myns4:add1 attribute="2">..</myns4:add1>
          <myns4:add2>..</myns4:add2>
          <myns4:add3>
            <myns5:asdf>..</myns5:asdf>
            <myns5:qwe>..</myns5:qwe>
          </myns4:add3>
          <myns4:add4>..</myns4:add4>
        </myns3:address>
        <myns2:query>..</myns2:query>
    </myns1:taskListResponse>

我使用下面的 XSLT 来做:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns1="http://www.w3.org/TR/html4/1"
    xmlns:myns2="http://www.w3.org/TR/html4/2"
    xmlns:myns3="http://www.w3.org/TR/html4/3"
    xmlns:myns4="http://www.w3.org/TR/html4/4"
    xmlns:myns5="http://www.w3schools.com/furniture"
    xmlns:ns1="http://www.w3.org/TR/html4/1"
    xmlns:ns2="http://www.w3.org/TR/html4/2"
    xmlns:ns3="http://www.w3.org/TR/html4/3"
    xmlns:ns4="http://www.w3.org/TR/html4/4"
    xmlns:ns5="http://www.w3schools.com/furniture">

    <xsl:template match="*">
      <myns1:taskListResponse>
        <xsl:apply-templates/>
      </myns1:taskListResponse>
    </xsl:template>

    <xsl:template match="ns2:*">
      <xsl:element name="myns2:{local-name()}">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>

    <xsl:template match="ns3:*">
      <xsl:element name="myns3:{local-name()}">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>

    <xsl:template match="ns4:*">
      <xsl:element name="myns4:{local-name()}">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>

    <xsl:template match="ns5:*">
      <xsl:element name="myns5:{local-name()}">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>

</xsl:stylesheet>

所有命名空间都根据需要正确转换为另一组,但不会复制字段中的属性。在这个例子中,我只定义了两个属性,但还有更多的属性。基本上,我想复制整个文档,只是要替换的命名空间。你能帮我解决这个问题吗?

【问题讨论】:

    标签: xml xslt namespaces


    【解决方案1】:
    <xsl:apply-templates/>
    

    等价于

    <xsl:apply-templates select="child::node()"/>
    

    并且child::包含属性。如果你添加一个额外的模板

    <xsl:template match="@*">
      <xsl:copy/>
    </xsl:template>
    

    然后您可以将所有apply-templates 调用更改为

    <xsl:apply-templates select="@*|node()" />
    

    复制属性以及应用元素模板。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多