9

I have an if test where I want to display the content of the 'year' property with a comma when the property has values. This isn't working so I would be thankful for suggestions.

<xsl:if test="year != null">
     <xsl:value-of select="year"/>,
</xsl:if> 
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
Cecil Theodore
  • 9,549
  • 10
  • 31
  • 37

3 Answers3

11

You can check year element presence simply using this expression:

<xsl:if test="year">

If you want to check that year element isn't empty:

<xsl:if test="year != ''">
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • how do i check if the element is empty? , i've tried this but it does not work.. Any suggestions? Thank you – codingbbq Jan 27 '14 at 09:57
0

I prefer to use the not function

not() Function — Returns the negation of its argument. If the argument is not a boolean value already, it is converted to a boolean value using the rules described in the boolean() function entry.

Example:

To demonstrate the not() function, we’ll use the same stylesheet and XML document we used for the boolean() function.

<xsl:choose>
    <xsl:when test="not(year='')">
        <xsl:value-of select="year"/>
    </xsl:when>
    <xsl:otherwise>
        <xsl:text>The element year is NULL</xsl:text>
    <xsl:otherwise>
</xsl:choose>

Same for the if statement:

<xsl:if test="not(year='')">
    <xsl:value-of select="year"/>
</xsl:if>
<xsl:if test="year=''">
    <xsl:text>The element year is NULL</xsl:text>
</xsl:if>
NvrKill
  • 327
  • 2
  • 16
0
<xsl:if test="year != null">    
     <xsl:value-of select="year"/>,    
</xsl:if>

This would be true() if there is at least one year child of the current node and one null child of the current node whose string values aren't equal.

Most probably there isn't any null element in the XML document that you haven't shown...

Use:

<xsl:if test="year"> 
     <xsl:value-of select="concat(year, ',')"/>
</xsl:if>  
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431