Skip an element when copying the whole node in xslt

六月ゝ 毕业季﹏ 提交于 2019-12-12 00:55:24

问题


Is it possible to skip an element from a node? For example we have node as Test and it has child elements x, y,z. I want to copy the whole Test node but don't want z element in the final result. Can we use not() in copy-of select? I tried but it didn't work.

Thanks.


回答1:


No, <xsl:copy-of> gives you no control over what happens inside what you are copying. That's what an identity template with selective omissions is for:

<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>

When applied to this XML:

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

The result is:

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

</Test>


来源:https://stackoverflow.com/questions/22578937/skip-an-element-when-copying-the-whole-node-in-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!