XSLT: check if any element group has a child element with the 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 of the "Data" elements contains a child of Item1 with a value of "A". I also want to do this once, even if several โ€œDataโ€ criteria match the criteria.

I think I need to write the <xsl:if test=""> operator to return true if any Data / Item1 contains the value "A".

Does anyone know how to do this with an if statement or in any other way?

Thanks in advance:)

for -Alex-

+4
source share
1 answer
 <xsl:template match="AllMyDataz"> <xsl:if test="Data/Item1[.='A']"> <!-- now do something --> </xsl:if> </xsl:template> 

Data/Item1[.='A'] selects all matching <Item1> nodes, resulting in a node-set.

When node-set is used in a boolean context, it evaluates to true if it is not empty and false if it is empty. Exactly what you wanted.

+6
source

Source: https://habr.com/ru/post/1303149/


All Articles