xsl:if at least one child node exists

38,674

Solution 1

Firstly, be careful with your terminology here. Do you mean "node" or do you mean "element". A node can be an element, comment, text or processing-instruction.

Anyway, if you do mean element here, to check at least one child element exists, you can just do this (assuming you are positioned on the node element in this case.

<xsl:if test="*">

Your comment suggests only "node1" can pass the if condition, so to check the existence of a specific element, do this

<xsl:if test="node1">

Solution 2

In the context of the node you are testing, this should work to test whether a node has child elements:

<xsl:if test="*">
  Only node1 can pass the if condition
</xsl:if>

If you actually meant nodes (which would include text nodes), then this would work to include text nodes:

<xsl:if test="node()">
  Only node1 can pass the if condition
</xsl:if>

But <node> would also pass this test (<node2> wouldn't). I assumed you were only speaking in the context of <node>'s child nodes, but perhaps not?

Share:
38,674
Tran Ngu Dang
Author by

Tran Ngu Dang

Geek forever !!!

Updated on September 28, 2020

Comments

  • Tran Ngu Dang
    Tran Ngu Dang almost 4 years
    <node>
       <node1><node11/></node1>
       <node2/>
    </node>
    

    I want my XSLT to check

    <xsl:if test="If at least 1 child node exists">
      Only node1 can pass the if condition
    </xsl:if>
    

    Thanks for any reply.

  • Tran Ngu Dang
    Tran Ngu Dang over 11 years
    Thanks for your answer. I got a confusion among those terms. Will be more carefull next time