【问题标题】:Copy xml with xslt skipping top level nodes使用 xslt 跳过顶级节点复制 xml
【发布时间】:2012-03-28 18:30:51
【问题描述】:

如何复制 xml 文档跳过一些顶级节点。例如:

输入:

<root>
 <subroot>
   <nodeX id="1">
     <!-- inner structure -->
   </nodeX>
   <nodeX id="2">
     <!-- inner structure -->
   </nodeX>
   <!-- other nodes -->
  </subroot>
<root>

输出:

   <nodeX id="1">
     <!-- inner structure -->
   </nodeX>
   <nodeX id="2">
     <!-- inner structure -->
   </nodeX>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:
    <xsl:template match="@* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="root | subroot">
      <xsl:apply-templates/>
    </xsl:template>
    

    应该。如果您想要或需要更通用的东西,请制作第二个模板

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

    【讨论】:

    • 是否可以跳过其他属于根子节点的节点?这些节点匹配 /root/subrootX|/root/subrootY 但我不想复制它们。
    • 只需将您不想复制的任何模式添加到第二个模板匹配模式,例如&lt;xsl:template match="/root | /root/subroot | /root/subrootX | /root/subrootY"&gt;&lt;xsl:apply-templates/&gt;&lt;/xsl:template&gt;。或者,如果您知道不想复制任何根元素的任何子元素,请使用我已经发布的通用 match="/* | /*/*"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多