Reputation:
Consider the following XML:
<AllMyDataz>
<Data>
<Item1>A</Item1>
</Data>
<Data>
<Item1>B</Item1>
</Data>
<Data>
<Item1>A</Item1>
</Data>
</AllMyDataz>
In my transformation I only want to do something if any of the "Data" elements contain a child element Item1 with the value of "A". I also only want to do this one time, even if multiple "Data" elements fit the criteria.
I think I need to write a <xsl:if test="">
statement to return true if any Data/Item1 contains the value "A".
Does anyone know how to do this with an if statement or any other way?
Thank you in advance :)
-Alex
Upvotes: 4
Views: 4646
Reputation: 338208
<xsl:template match="AllMyDataz">
<xsl:if test="Data/Item1[.='A']">
<!-- now do something -->
</xsl:if>
</xsl:template>
The Data/Item1[.='A']
selects all the matching <Item1>
nodes, resulting in a node-set.
When a node-set is used in Boolean context, it evaluates to true
if it is non-empty and to false
if it is empty. Exactly what you wanted.
Upvotes: 8