If condition in XSL not working

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.
rajivrajAsked:
Who is Participating?
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
Note that I would use a substring for this task, and no conditions at all
        <xsl:variable name="len" select="string-length(Extn/@ExtnFromCaseNo)"/>
        <xsl:variable name="zeroes">000</xsl:variable>
        <xsl:variable name="InvoiceCaseNo" select="concat($FormattedShipmentNo,'   ', substring($zeroes, $len + 1))"/>
From the $zeroes, I take exactly the ones I need, based on the value of $len
I recommend you to adopt this style

cheers

Geert
0
 
digitalZoCommented:
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>
0
 
digitalZoCommented:
 <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>
0
 
Geert BormansInformation ArchitectCommented:
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"/>
0
 
Geert BormansInformation ArchitectCommented:
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>
0
All Courses

From novice to tech pro — start learning today.