Xsl and excluding a child node


I was writing an xsl stylesheet to do xml transformation and I wanted to eliminate nodes based on their children, specifically if the parent nodes were the same. It was taking way to long to figure out how to exclude an xml node based on its descendant or child and poking around on the internet. I finally got it working and thought it might save others some time. Here is the xml and xsl:

xml:

<test>

  <parent>

    <mychild>hello</mychild>

  </parent>

  <parent>

    <ourchild>world</ourchild>

  </parent>

</test>

xsl:

<?xml version=1.0?>

<xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform>

 

  <xsl:template match=test>

    <output>

      <xsl:apply-templates select=parent/>

    </output>

  </xsl:template>

 

 

  <xsl:template match=parent>

    <xsl:if test=not(./mychild)>

      <correctChild>

        <xsl:value-of select=./>

      </correctChild>

    </xsl:if>

  </xsl:template>

</xsl:stylesheet>

result:

<?xml version=1.0 encoding=utf-16 ?>

<output>

  <correctChild>world</correctChild>

</output>

  1. No comments yet.
(will not be published)
  1. No trackbacks yet.