tags:

views:

50

answers:

1

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

+3  A: 
<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.

Tomalak
This "If" condition is having an existing value of <Item1>... If suppose I've to check today's date in this "If" condition in xml. Like: <xsl:if on=(Today's Date)> for displaying only those elements which have today's date in my xml. Where <on>08/mar/2010</on> is in the xml file. Then how should I do that?
gsvirdi
@gsvirdi: This is a separate question. Since you've already noticed that yourself, so all I can do is link to it. :) http://stackoverflow.com/questions/2400622/working-with-xml-xsl
Tomalak