Question

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.

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top