Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xsl:if at least one child node exists

Tags:

xslt

xpath

<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.

like image 269
Tran Ngu Dang Avatar asked Jan 21 '13 09:01

Tran Ngu Dang


2 Answers

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">
like image 65
Tim C Avatar answered Sep 29 '22 20:09

Tim C


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?

like image 25
JLRishe Avatar answered Sep 29 '22 20:09

JLRishe