Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
11 replies Latest Post - ‏2013-08-30T10:21:47Z by mtz
mtz
mtz
44 Posts
ACCEPTED ANSWER

Pinned topic Getting NaN when performing arithmetic operation on xslt variables

‏2013-08-15T15:24:23Z |

Hi,

Could you please explain to me how xslt variables behave when arithmetic operations are performed on them. PFB the sample xml and xslt for which I am getting NaN output.

XML:

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="varshney.xsl" ?>
<ad>
<a1>2</a1>
<a2>3</a2>
</ad>

 

XSLT:

 

<?xml version="1.0" encoding="ISO-8859-1"?>
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
<xsl:output method="xml"/>
 
<xsl:template match="/">
 
<content>
 
<aa><xsl:variable name ="A" select="ad/a1"/></aa>
 
<bb><xsl:variable name ="B" select="ad/a2"/></bb>
 
<cc><xsl:variable name ="C" select="$A + $B"/></cc>
 
<dd><xsl:variable name ="D" select="$B - $A"/></dd>
 
<ee><xsl:variable name ="E" select="$A * $B"/></ee>
 
<ff><xsl:variable name ="F" select="$B div $A"/></ff>
 
<Result>
 
<a> <xsl:variable name ="Sum" select="$C"/><xsl:value-of select="$C"/> </a>  <br/>
 
<b> <xsl:variable name ="Diff" select="$D"/><xsl:value-of select="$D"/> </b> <br/>
 
<c> <xsl:variable name ="Mul" select="$E"/><xsl:value-of select="$E"/> </c>  <br/>
 
<d> <xsl:variable name ="Div" select="$F"/><xsl:value-of select="$F"/> </d>  <br/>
 
<e> <xsl:element name ="Sum1">Sum1:-<xsl:value-of select="$Sum + $Diff"/></xsl:element> </e> <br/>
 
<f> <xsl:element name ="Diff1">Diff1:-<xsl:value-of select="$Sum - $Diff"/></xsl:element> </f> <br/>
 
<g> <xsl:element name ="Mul1">Mul1:-<xsl:value-of select="$Sum * $Diff"/></xsl:element> </g> <br/>
 
<h> <xsl:element name ="Divl1">Div1:-<xsl:value-of select="$Sum div $Mul"/></xsl:element> </h> <br/>
 
</Result>
 
</content>
 
</xsl:template>
 
</xsl:stylesheet> 
  • HermannSW
    HermannSW
    4488 Posts
    ACCEPTED ANSWER

    Re: Getting NaN when performing arithmetic operation on xslt variables

    ‏2013-08-15T17:43:26Z  in response to mtz

    I do not have access to my DataPower boxes the next 1.5 weeks.
    But I did store your stylesheet and input file on my private webserver.

    Accessing the XML file executing the stylesheet does not result in NaN, at least in this old IE web browser of Youth hostel web terminal (DJH List/Sylt, Germany):
    http://stamm-wilbrandt.de/en/forum/varschney.xml
     

    Hermann.

    • mtz
      mtz
      44 Posts
      ACCEPTED ANSWER

      Re: Getting NaN when performing arithmetic operation on xslt variables

      ‏2013-08-16T05:58:08Z  in response to HermannSW

      Hi Hermann,

      Yea compiling the xslt isn't causing any issues; one thing that I am sure is I am not assigning a numeric value somehow to the variable and still performing the arithmetic operation on it. But still am not getting where is it going wrong, that's why I am more interested in understanding the concept of assigning values to xslt variabled than the xslt itself.

       

      Mohit

      • HermannSW
        HermannSW
        4488 Posts
        ACCEPTED ANSWER

        Re: Getting NaN when performing arithmetic operation on xslt variables

        ‏2013-08-16T06:38:52Z  in response to mtz

        Hi Mohit,

        please click on the link in my previous posting, and you will see that there is not a single NaN.
        Clicking on the link will execute your stylesheet (referenced by xml-stylesheet).

        Please post the result you complain about here (with NaN), and how you did get that.

        XSLT variable (reference) and value does not make a difference.

        Please see the XPath spec on how  numbers and other data types get casted to perform arithmetic operations:
        http://www.w3.org/TR/xpath/

        Hermann.

        Updated on 2013-08-16T06:39:35Z at 2013-08-16T06:39:35Z by HermannSW
        • mtz
          mtz
          44 Posts
          ACCEPTED ANSWER

          Re: Getting NaN when performing arithmetic operation on xslt variables

          ‏2013-08-16T07:01:35Z  in response to HermannSW

          Hi Hermann,

          PFB the rsult I am getting when testing through xslt coproc:

           

          <?xml version="1.0" encoding="UTF-8"?>
          <content><aa/><bb/><cc/><dd/><ee/><ff/><Result><a/><br/><b/><br/><c/><br/><d/><br/><e><Sum1>Sum1:-NaN</Sum1></e><br/><f><Diff1>Diff1:-NaN</Diff1></f><br/><g><Mul1>Mul1:-NaN</Mul1></g><br/><h><Divl1>Div1:-NaN</Divl1></h><br/></Result></content>

           

          Thanks,

          Mohit

          • swlinn
            swlinn
            1346 Posts
            ACCEPTED ANSWER

            Re: Getting NaN when performing arithmetic operation on xslt variables

            ‏2013-08-16T14:41:23Z  in response to mtz

            Hi Mohit,

            I think you're running into variable scope issues.  I changed your code to move all of the variables outside of the individual output elements and it works fine.  Here's the xsl:

            <xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                
                <xsl:output method="xml" />
                <xsl:template match="/">
                    <xsl:variable name="A" select="ad/a1" />
                    <xsl:variable name="B" select="ad/a2" />
                    <xsl:variable name="C" select="$A + $B" />
                    <xsl:variable name="D" select="$B - $A" />
                    <xsl:variable name="E" select="$A * $B" />
                    <xsl:variable name="F" select="$B div $A" />
                    <content>
                        <aa>
                            <xsl:value-of select="ad/a1" />
                        </aa>
                        <bb>
                            <xsl:value-of select="ad/a2" />
                        </bb>
                        <Result>
                            <a>
                                <xsl:value-of select="$C" />
                            </a>
                            <br />
                            <b>
                                <xsl:value-of select="$D" />
                            </b>
                            <br />
                            <c>
                                <xsl:value-of select="$E" />
                            </c>
                            <br />
                            <d>
                                <xsl:value-of select="$F" />
                            </d>
                            <br />
                            <e>
                                <xsl:element name="Sum1">
                                    Sum1:-
                                    <xsl:value-of select="$C + $D" />
                                </xsl:element>
                            </e>
                            <br />
                            <f>
                                <xsl:element name="Diff1">
                                    Diff1:-
                                    <xsl:value-of select="$C - $D" />
                                </xsl:element>
                            </f>
                            <br />
                            <g>
                                <xsl:element name="Mul1">
                                    Mul1:-
                                    <xsl:value-of select="$C * $D" />
                                </xsl:element>
                            </g>
                            <br />
                            <h>
                                <xsl:element name="Divl1">
                                    Div1:-
                                    <xsl:value-of select="$C div $E" />
                                </xsl:element>
                            </h>
                            <br />
                        </Result>
                    </content>
                </xsl:template>
            </xsl:stylesheet>

            and the result

            <?xml version="1.0" encoding="UTF-8"?>
            <content>
                <aa>2</aa>
                <bb>3</bb>
                <Result>
                    <a>5</a>
                    <br />
                    <b>1</b>
                    <br />
                    <c>6</c>
                    <br />
                    <d>1.5</d>
                    <br />
                    <e>
                        <Sum1>
                            Sum1:-
                            6
                        </Sum1>
                    </e>
                    <br />
                    <f>
                        <Diff1>
                            Diff1:-
                            4
                        </Diff1>
                    </f>
                    <br />
                    <g>
                        <Mul1>
                            Mul1:-
                            5
                        </Mul1>
                    </g>
                    <br />
                    <h>
                        <Divl1>
                            Div1:-
                            0.8333333333333334
                        </Divl1>
                    </h>
                    <br />
                </Result>
            </content>

            I also just used your variables C, D, and E instead of Sum, Diff, Mult which were simply set to the value of the former.

            Regards,
            Steve

            • mtz
              mtz
              44 Posts
              ACCEPTED ANSWER

              Re: Getting NaN when performing arithmetic operation on xslt variables

              ‏2013-08-21T14:49:12Z  in response to swlinn

              Hi Steve,

              Thanks for the solution but I am not getting what do you mean when you there is an issue with variable scope here? Why am I not able to print the values of the variables when assigning them to a different variable? How does a variable behave in this regard?

               

              Thanks,

              Mohit

              • swlinn
                swlinn
                1346 Posts
                ACCEPTED ANSWER

                Re: Getting NaN when performing arithmetic operation on xslt variables

                ‏2013-08-21T15:05:51Z  in response to mtz

                Hi Mohit,

                xsl variable have scope, ie, they are only accessible within the element that they are created.  In your original code ...

                <aa>

                       <xsl:variable name ="A" select="ad/a1"/>

                       <xsl:message>this works as the code is within the scope of the aa element, A=<xsl:value-of select="$A" /></xsl:message>

                </aa>

                <xsl:message>this DOES NOT works as the code is outside the scope of the aa element, A=<xsl:value-of select="$A" /></xsl:message>

                the latter xsl:value will not find variable A as it only exists within the aa element.  This is similar to other languages.  I believe variables declared in Java that are within some grouping only exist within that grouping (for example, an if block).

                Regards,
                Steve

                • mtz
                  mtz
                  44 Posts
                  ACCEPTED ANSWER

                  Re: Getting NaN when performing arithmetic operation on xslt variables

                  ‏2013-08-21T16:20:30Z  in response to swlinn

                  Got it Steven. This is a big relief to me now that i know this variable thing in xslt works the same way as in other languages..

                  Thnx a lot, appreciate that...

                   

                  Mohit

                  • HermannSW
                    HermannSW
                    4488 Posts
                    ACCEPTED ANSWER

                    Re: Getting NaN when performing arithmetic operation on xslt variables

                    ‏2013-08-21T17:22:31Z  in response to mtz

                    Oops, oversaw what Steven found.

                    From the spec:
                    http://www.w3.org/TR/xslt#local-variables

                    "... xsl:variable is allowed anywhere within a template that an instruction is allowed. In this case, the binding is visible for all following siblings and their descendants. ..."

                    So the browser XSLT not returning NaN when clicking on my link in previous posting is buggy.
                    DataPower XSLTprocessor was right in returning NaN before Steven's stylesheet modifications.

                    Hermann.

                  • swlinn
                    swlinn
                    1346 Posts
                    ACCEPTED ANSWER

                    Re: Getting NaN when performing arithmetic operation on xslt variables

                    ‏2013-08-21T17:28:44Z  in response to mtz

                    Well, xsl variables work as other languages in this respect (scope).  Just remember that xsl variables are immutable, so they can only be set to a value once, which is not like other languages.

                    Regards,

                    Steve

                    • mtz
                      mtz
                      44 Posts
                      ACCEPTED ANSWER

                      Re: Getting NaN when performing arithmetic operation on xslt variables

                      ‏2013-08-30T10:21:47Z  in response to swlinn

                      Even though browser had bug Hermann, it helped in knowing the difference in xslt behaviour!!

                      Steve, thanks again for immutable info; wasn't aware of that either..

                       

                      Regards,

                      Mohit