KP65
KP65

Reputation: 13585

XSLT If condition Help

I have an XML and XSLT. I want to use the if condition to check if an element is 0, if it is then hide it i.e display nothing. IF 0 is found, simply display a blank cell.

Here is the basic concept:

XML

 <Sheet1>
     <Build>0</Build>
     <Item>X</Item>
     <T1>0:06:00</T1>
     <T2>0:14:15</T2>
     <T3>0:07:22</T3>
 </Sheet1>

XSLT

<table border="1">
    <tr bgcolor="#dccdc">
        <th align="center">Build</th>
        <th align="center">Item</th>
        <th align="center">T1</th>
        <th align="center">T2</th>
        <th align="center">T3</th>
    </tr>  

    <xsl:for-each select="Sheet1">
        <tr>
            <td bgcolor="#F2F5A9">
                <xsl:value-of select="Build" />
            </td>
            <td bgcolor="#F2F5A9">
                <xsl:value-of select="Item" />
            </td>
            <td bgcolor="#F2F5A9">
                <xsl:value-of select="T1" />
            </td>
            <td bgcolor="#F2F5A9">
                <xsl:value-of select="T2" />
            </td>
            <td bgcolor="#F2F5A9">
                <xsl:value-of select="T3" />
            </td>
         </tr>
     </xsl:for-each>
</table>

What I want to do is when Build element = 0 i want it to print nothing, so i tried putting an xsl:if around the item in the XSLT:

<xsl:if test="Build!='0'">
    <td bgcolor="#F2F5A9">
        <xsl:value-of select="Build" />
    </td>
</xsl:if>

but this does not seem to work, i get a blank output file.

any ideas?

Upvotes: 0

Views: 1362

Answers (2)

Emiliano Poggi
Emiliano Poggi

Reputation: 24816

A correct approach is also (fragment):

  <td bgcolor="#F2F5A9">
    <xsl:if test="Build[text()!='0']">
      <xsl:value-of select="Build" />
    </xsl:if>
  </td>

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163262

Simplest solution is to write

    <td bgcolor="#F2F5A9">
        <xsl:value-of select="Build[. != 0]" />
    </td>

Upvotes: 2

Related Questions