XSLT: Check if any group of elements have a child element with a specified value
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 开发者_Python百科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
<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.
精彩评论