Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT otherwise block does not execute

Tags:

xml

xslt

xslt-1.0

XML

<Categories>
  <category>
    <blog>ABC</blog>
    <link>open</link>
    <link1>close</link1>
  </category>
</Categories>

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="category">

  <xsl:variable name="openCloseValidation">
     <xsl:value-of select="link" />

  </xsl:variable>

    <xsl:variable name="holidayValidation">
     <xsl:value-of select="link1" />

  </xsl:variable>

    <h1><xsl:value-of select="normalize-space($openCloseValidation)" /></h1>
    <h1><xsl:value-of select="normalize-space($holidayValidation)" /></h1>

    <xsl:choose>
      <xsl:when test="contains($openCloseValidation, 'open')">
        <xsl:if test="not(contains($holidayValidation, 'close'))">
          <xsl:value-of select="'true'" />
        </xsl:if>
      </xsl:when>

      <xsl:otherwise>
        <xsl:value-of select="'false'" />
      </xsl:otherwise>

  </xsl:choose>
</xsl:template>
</xsl:stylesheet>

the first condition does not satisfy but it doesn't go in to otherwise block and display false.

Can you let me know if something is wrong here?

like image 625
user2628187 Avatar asked Jan 21 '26 03:01

user2628187


1 Answers

The first xsl:when condition is satisfied, as contains($openCloseValidation, 'open') is true. But within your xsl:when you have an xsl:if and if that fails, then nothing will get output.

You should rewrite it as this....

<xsl:choose>
  <xsl:when test="contains($openCloseValidation, 'open') and not(contains($holidayValidation, 'close'))">
      <xsl:value-of select="'true'" />
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="'false'" />
  </xsl:otherwise>
</xsl:choose>

As an aside, it is much better to write your variable declarations like so...

<xsl:variable name="openCloseValidation" select="link" />
like image 125
Tim C Avatar answered Jan 23 '26 21:01

Tim C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!