문제

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.

도움이 되었습니까?

해결책

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top