How to implement if-else statement in XSLT?

XmlXsltIf Statement

Xml Problem Overview


I am trying to implement an if -else statement in XSLT but my code just doesn't parse. Does anyone have any ideas?

  <xsl:variable name="CreatedDate" select="@createDate"/>
  <xsl:variable name="IDAppendedDate" select="2012-01-01" />
  <b>date: <xsl:value-of select="$CreatedDate"/></b> 

  <xsl:if test="$CreatedDate > $IDAppendedDate">
    <h2> mooooooooooooo </h2>
  </xsl:if>
  <xsl:else>
    <h2> dooooooooooooo </h2>
  </xsl:else>

Xml Solutions


Solution 1 - Xml

You have to reimplement it using <xsl:choose> tag:

<xsl:choose>
  <xsl:when test="$CreatedDate > $IDAppendedDate">
    <h2> mooooooooooooo </h2>
  </xsl:when>
  <xsl:otherwise>
    <h2> dooooooooooooo </h2>
  </xsl:otherwise>
</xsl:choose>

Solution 2 - Xml

If statement is used for checking just one condition quickly. When you have multiple options, use <xsl:choose> as illustrated below:

   <xsl:choose>
     <xsl:when test="$CreatedDate > $IDAppendedDate">
       <h2>mooooooooooooo</h2>
     </xsl:when>
     <xsl:otherwise>
      <h2>dooooooooooooo</h2>
     </xsl:otherwise>
   </xsl:choose>

Also, you can use multiple <xsl:when> tags to express If .. Else If or Switch patterns as illustrated below:

   <xsl:choose>
     <xsl:when test="$CreatedDate > $IDAppendedDate">
       <h2>mooooooooooooo</h2>
     </xsl:when>
     <xsl:when test="$CreatedDate = $IDAppendedDate">
       <h2>booooooooooooo</h2>
     </xsl:when>
     <xsl:otherwise>
      <h2>dooooooooooooo</h2>
     </xsl:otherwise>
   </xsl:choose>

The previous example would be equivalent to the pseudocode below:

   if ($CreatedDate > $IDAppendedDate)
   {
       output: <h2>mooooooooooooo</h2>
   }
   else if ($CreatedDate = $IDAppendedDate)
   {
       output: <h2>booooooooooooo</h2>
   }
   else
   {
       output: <h2>dooooooooooooo</h2>
   }

Solution 3 - Xml

If I may offer some suggestions (two years later but hopefully helpful to future readers):

  • Factor out the common h2 element.
  • Factor out the common ooooooooooooo text.
  • Be aware of new XPath 2.0 if/then/else construct if using XSLT 2.0.

XSLT 1.0 Solution (also works with XSLT 2.0)

<h2>
  <xsl:choose>
    <xsl:when test="$CreatedDate > $IDAppendedDate">m</xsl:when>
    <xsl:otherwise>d</xsl:otherwise>
  </xsl:choose>
  ooooooooooooo
</h2>

XSLT 2.0 Solution

<h2>
   <xsl:value-of select="if ($CreatedDate > $IDAppendedDate) then 'm' else 'd'"/>
   ooooooooooooo
</h2>

Solution 4 - Xml

Originally from this blog post. We can achieve if else by using below code

<xsl:choose>
    <xsl:when test="something to test">

    </xsl:when>
    <xsl:otherwise>

    </xsl:otherwise>
</xsl:choose>

So here is what I did

<h3>System</h3>
	<xsl:choose>
		<xsl:when test="autoIncludeSystem/autoincludesystem_info/@mdate"> <!-- if attribute exists-->
			<p>
				<dd><table border="1">
					<tbody>
						<tr>
							<th>File Name</th>
							<th>File Size</th>
							<th>Date</th>
							<th>Time</th>
							<th>AM/PM</th>
						</tr>
						<xsl:for-each select="autoIncludeSystem/autoincludesystem_info">
						    <tr>
								<td valign="top" ><xsl:value-of select="@filename"/></td>
								<td valign="top" ><xsl:value-of select="@filesize"/></td>
								<td valign="top" ><xsl:value-of select="@mdate"/></td>
								<td valign="top" ><xsl:value-of select="@mtime"/></td>
								<td valign="top" ><xsl:value-of select="@ampm"/></td>
							</tr>
						</xsl:for-each>
					</tbody>
				</table>
				</dd>
			</p>
		</xsl:when>
		<xsl:otherwise> <!-- if attribute does not exists -->
		    <dd><pre>
				<xsl:value-of select="autoIncludeSystem"/><br/>
			</pre></dd>	<br/>
		</xsl:otherwise>
	</xsl:choose>

My Output

enter image description here

Solution 5 - Xml

The most straight-forward approach is to do a second if-test but with the condition inverted. This technique is shorter, easier on the eyes, and easier to get right than a choose-when-otherwise nested block:

<xsl:variable name="CreatedDate" select="@createDate"/>
     <xsl:variable name="IDAppendedDate" select="2012-01-01" />
     <b>date: <xsl:value-of select="$CreatedDate"/></b> 
     <xsl:if test="$CreatedDate &gt; $IDAppendedDate">
        <h2> mooooooooooooo </h2>
     </xsl:if>
     <xsl:if test="$CreatedDate &lt;= $IDAppendedDate">
        <h2> dooooooooooooo </h2>
     </xsl:if>

Here's a real-world example of the technique being used in the style-sheet for a government website: http://w1.weather.gov/xml/current_obs/latest_ob.xsl

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFunkyView Question on Stackoverflow
Solution 1 - Xmlpx1mpView Answer on Stackoverflow
Solution 2 - XmlInfantPro'Aravind'View Answer on Stackoverflow
Solution 3 - XmlkjhughesView Answer on Stackoverflow
Solution 4 - XmlAabinGunzView Answer on Stackoverflow
Solution 5 - XmlRaymond HettingerView Answer on Stackoverflow