compact XSLT code to drop N number of tags if all are null.

Posted by infant programmer on Stack Overflow See other posts from Stack Overflow or by infant programmer
Published on 2010-03-24T12:06:24Z Indexed on 2010/03/24 12:23 UTC
Read the original article Hit count: 208

Filed under:
|
|

This is my input xml:

<root>
   <node1/>
   <node2/>
   <node3/>
   <node4/>
   <othertags/>
</root>

The output must be:

<root>
   <othertags/>
</root>

if any of the 4 nodes isn't null then none of the tags must be dropped.
example:

<root>
   <node1/>
   <node2/>
   <node3/>
   <node4>sample_text</node4>
   <othertags/>
</root>

Then the output must be same as input xml.

<root>
   <node1/>
   <node2/>
   <node3/>
   <node4>sample_text</node4>
   <othertags/>
</root>

This is the XSL code I have designed ::

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
<xsl:template match="/root/node1[.='' and ../node2/.='' and ../node3/.='' and ../node4/.='']
             |/root/node2[.='' and ../node1/.='' and ../node3/.='' and ../node4/.='']
             |/root/node3[.='' and ../node1/.='' and ../node2/.='' and ../node4/.='']
             |/root/node4[.='' and ../node1/.='' and ../node2/.='' and ../node3/.='']"/>

As you can see the code requires more effort and becomes more bulky as the number of nodes increase. Is there any alternative way to overcome this bottleneck?

© Stack Overflow or respective owner

Related posts about xsl

Related posts about xslt