tags:

views:

87

answers:

3

I have the following collection in an XML document:

<events>
   <event>
      <type>Downloaded</type>
      <result>Sucess</result>
   </event>
   <event>
      <type>Processed</type>
      <result>Sucess</result>
   </event>
</events>

Now in my XSLT I have a table with a TD - I want the value of this TD to represent the status of the events. If an event exists for processed and result is true, then I want the value of this TD to be Processed, likewise, if processed doesn't exist, then if downloaded exists and status is success, then I want the value of the TD to be downloaded...

Don't expect full code, just a sample on how to add some programming logic to XSLT.

What I really need to check for ... is

Does Element Event Exist with type = "Processed".... if not... then.... I'll figure the rest out.....

+1  A: 

You can add if/else if logic to XSLT with <xsl:if>

There is also the ability to have something like a switch statement with <xsl:choose>, which includes the capability do do 'else' behaviour.

These constructs take a test attribute, in which you specify the condition. Here's a nice writeup on useful getting-started tests.

It's really something you have to play with to get used to, but these website links will give you a great start.

Example: given your document a template like:

<xsl:template match="/">
    <xsl:for-each select="events/event">
        <xsl:choose>
            <xsl:when test="type/text() = 'Processed'">
                <xsl:value-of select="result"></xsl:value-of>
            </xsl:when>    
        </xsl:choose>
    </xsl:for-each>
</xsl:template>

Will produce the text 'Sucess'.

Brabster
However, to get if/*else* logic in XSLT 1.0 you would typically use `xsl:when` and `xsl:otherwise` (`xsl:if` does not have an else branch).
0xA3
Please provide a quick example that checks for the existance of an element with certain values?
JL
Note using the text() function there - if you don't do that, the text you are checking is the concatenated value of all the child elements of the element in question. Confuses the hell out of people.
Brabster
<xsl:choose> <xsl:when test="events/event[type='Processed']"> <!-- this condition will be true if there exists an event that satisfies the predicate "has a type child whose text value is 'Processed'" --> </xsl:when> <xsl:otherwise> <!-- this is your "else" case --> </xsl:otherwise></xsl:choose>I actually don't think you want to use the xsl:for-each here, because you are concerned if there is "at least one" event that matches a condition, correct?
carolclarinet
+1  A: 

Untested, and I'm a bit confused by the logic you're trying to implement, but try starting with this:

<xsl:template match="/">
  <table>
    <xsl:apply-templates select="events/event" />
  </table>
</xsl:template>

<xsl:template match="event">
  <xsl:if test="type = 'Processed'">
    <tr>
      <td>
        <xsl:value-of select="result" />
      </td>
    </tr>
  </xsl:if>
</xsl:template>
Keith
Like @JBKing says, if you need an "else" condition, you will need to use <xsl:choose>
Keith
+1  A: 

xsl:choose is another option. From that link:

<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <xsl:choose>
          <xsl:when test="price &gt; 10">
            <td bgcolor="#ff00ff">
            <xsl:value-of select="artist"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td><xsl:value-of select="artist"/></td>
          </xsl:otherwise>
        </xsl:choose>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

The xsl:if doen't have an else functionality.

JB King