在xslt中复制整个节点时跳过元素

时间:2014-03-22 14:34:05

标签: xslt xpath xslt-1.0

是否可以跳过节点中的元素?例如,我们将节点设为Test,它具有子元素xyz。我想复制整个Test节点,但不希望在最终结果中使用z元素。我们可以在not()中使用copy-of select吗?我试过但它没有用。

感谢。

1 个答案:

答案 0 :(得分:3)

不,<xsl:copy-of>让你无法控制你正在复制的内容。这就是具有选择性遗漏的身份模板的用途:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <!-- Identity template -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

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

  <!-- Omit z from the results-->
  <xsl:template match="z" />
</xsl:stylesheet>

应用于此XML时:

<n>
  <Test>
    <x>Hello</x>
    <y>Heeelo</y>
    <z>Hullo</z>
  </Test>
</n>

结果是:

<Test>
  <x>Hello</x>
  <y>Heeelo</y>

</Test>