We help IT Professionals succeed at work.

If condition in XSL not working

1,930 Views
Last Modified: 2013-11-18
Hello,

The XSL below doesnt seem to work:
                        <xsl:variable name="len" select="string-length(Extn/@ExtnFromCaseNo)"/>
                        <xsl:if test="$len = 1">
                              <xsl:variable name="InvoiceCaseNo" select="concat($FormattedShipmentNo,'   00')"/>
                        </xsl:if>
                        <xsl:if test="$len = 2">
                              <xsl:variable name="InvoiceCaseNo" select="concat($FormattedShipmentNo,'   0')"/>
                        </xsl:if>
                        <xsl:if test="$len &gt; 2">
                              <xsl:variable name="InvoiceCaseNo" select="concat($FormattedShipmentNo,'   ')"/>

The if tests never match!

please suggest.

Thanks.
Comment
Watch Question

Instead of 'IF' try 'WHEN' and 'OTHERWISE'.

      <xsl:when test="$len = 1">
                              <xsl:variable name="InvoiceCaseNo" select="concat($FormattedShipmentNo,'   00')"/>
                        </xsl:when>
                        <xsl:when test="$len = 2">
                              <xsl:variable name="InvoiceCaseNo" select="concat($FormattedShipmentNo,'   0')"/>
                        </xsl:when>
                        <xsl:otherwise test="$len &gt; 2">
                              <xsl:variable name="InvoiceCaseNo" select="concat($FormattedShipmentNo,'   ')"/>
                       </xsl:otherwise>
 <xsl:when test="$len = 1">
                              <xsl:variable name="InvoiceCaseNo" select="concat($FormattedShipmentNo,'   00')"/>
                        </xsl:when>
                        <xsl:when test="$len = 2">
                              <xsl:variable name="InvoiceCaseNo" select="concat($FormattedShipmentNo,'   0')"/>
                        </xsl:when>
                        <xsl:when test="$len &gt; 2">
                              <xsl:variable name="InvoiceCaseNo" select="concat($FormattedShipmentNo,'   ')"/>
                       </xsl:when>
                        <xsl:otherwise></xsl:otherwise>
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
digitalZo is right that a three way xsl:if should be better replaced with a xsl:when construct
all the xsl:if will be evaluated one after the other
an xsl:when construct ends after the first test that is true, so a when construct is potentially a lot faster

DigitalZo makes three errors though
- an xsl:otherwise can't have a test attribute and is not needed in this case (you recovered from that in your second post)
- a series of xsl:when should be wrapped in an xsl:choose
- when evaluates the same as if, so it can't be the solution of this question

The real problem is the creation of the variables
Variables in XSLT are like constants, they can't vary,
When you create they take an initial value, and you can't override that value afterwards
Variables are also scoped, so the variables you create do exist with the correct value, but only inside the if

To do what you want, you need to do the test inside the variable declaration
This works
        <xsl:variable name="len" select="string-length(Extn/@ExtnFromCaseNo)"/>
        <xsl:variable name="InvoiceCaseNo">
            <xsl:if test="$len = 1">
                <xsl:value-of select="concat($FormattedShipmentNo,'   00')"/>
             </xsl:if>
            <xsl:if test="$len = 2">
                <xsl:value-of select="concat($FormattedShipmentNo,'   0')"/>
            </xsl:if>
            <xsl:if test="$len &gt; 2">
                <xsl:value-of select="concat($FormattedShipmentNo,'   ')"/>
            </xsl:if>
        </xsl:variable>

It might be that you are really not hitting a true test, because your XPath is out of context
and you don't test for zero length

try this to make sure
        <xsl:variable name="len" select="string-length(Extn/@ExtnFromCaseNo)"/>
        <xsl:variable name="InvoiceCaseNo">
            <xsl:if test="$len = 0">
                <xsl:value-of select="ALERT"/>
            </xsl:if>
            <xsl:if test="$len = 1">
                <xsl:value-of select="concat($FormattedShipmentNo,'   00')"/>
            </xsl:if>
            <xsl:if test="$len = 2">
                <xsl:value-of select="concat($FormattedShipmentNo,'   0')"/>
            </xsl:if>
            <xsl:if test="$len &gt; 2">
                <xsl:value-of select="concat($FormattedShipmentNo,'   ')"/>
            </xsl:if>
        </xsl:variable>
        <xsl:value-of select="$InvoiceCaseNo"/>
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Of course you would be better off with a when clause

        <xsl:variable name="InvoiceCaseNo">
            <xsl:choose>
                <xsl:when test="$len = 0">
                    <xsl:value-of select="ALERT"/>
                </xsl:when>
                <xsl:when test="$len = 1">
                    <xsl:value-of select="concat($FormattedShipmentNo,'   00')"/>
                </xsl:when>
                <xsl:when test="$len = 2">
                    <xsl:value-of select="concat($FormattedShipmentNo,'   0')"/>
                </xsl:when>
                <xsl:when test="$len &gt; 2">
                    <xsl:value-of select="concat($FormattedShipmentNo,'   ')"/>
                </xsl:when>
            </xsl:choose>
Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.