Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 24th, 2008, 04:06 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Variable scope in XSLT?

Hiya,

I am finding that approaching XSLT with the same logic as I would PHP/ASP classic is leaving me a little flat.

Having been assisted with he > issue I was experiencing here http://p2p.wrox.com/topic.asp?TOPIC_ID=74692

I have a new issue where my XSLT file works in Firefox but not in IE properly. The XSLT is like so and the xml is found in the linked topic above.


Code:
<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:param name="amount"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:param name="Domain"/>
<xsl:param name="remoteDomain"/>
<xsl:param name="UploadsFolder"/>
<xsl:template match="/currencies/currency">

<xsl:choose>
<xsl:when test="$from">
    <xsl:choose>
       <xsl:when test="$from='21'">                 
            <xsl:if test="currencyID=$to">
                <p class="calc">
                    <label class="currencyresultlabel">Result :</label>
                        <span><xsl:value-of select='format-number($amount * currencySellValue, "#.000")'/>  <xsl:value-of select="currencyCountry"/></span>

                        <img class="flag">
                          <xsl:attribute name="src">
                            <xsl:value-of select="concat($remoteDomain, $UploadsFolder, currencyIcon)" />
                           </xsl:attribute>
                        </img>
                        <br />
                        <label class="currencyresultlabel"></label> 1 GBP = <xsl:value-of select="currencySellValue" /> <xsl:value-of select="currencyCountry" />   
                 </p>

            </xsl:if>
       </xsl:when> 

         <xsl:when test="$from!='21'">                 
            <xsl:if test="currencyID=$from">
                <p class="calc">

                    <label class="currencyresultlabel">Result :</label>
                        <span><xsl:value-of select='format-number($amount div currencyBuyValue, "#.000")'/> GBP</span>

                        <img class="flag">
                          <xsl:attribute name="src">
                            <xsl:value-of select="concat($remoteDomain, $UploadsFolder, 'gb.gif')" />
                           </xsl:attribute>
                        </img>
                        <br /><label class="currencyresultlabel"></label><xsl:value-of select="currencyBuyValue" /><xsl:value-of select="currencyCountry" /> = 1 GBP

                 </p>
            </xsl:if>
       </xsl:when> 
    </xsl:choose>
</xsl:when>

<xsl:otherwise>
</xsl:otherwise>

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


<xsl:template match="/currencies/currencyComparison">

    <xsl:choose>

           <xsl:when test="$from='21' and $to='6'"> 

                <xsl:variable name="No1Price" select="/currencies/currencyComparison[currencyNaming='Dollar' and currencyProvider='No1']/currencyRate"/>
                <xsl:variable name="ThomasCookPrice" select="/currencies/currencyComparison[currencyNaming='Dollar' and currencyProvider='ThomasCook']/currencyRate"/>

                <xsl:variable name="MSPrice" select="/currencies/currencyComparison[currencyNaming='Dollar' and currencyProvider='MS']/currencyRate"/>
                <xsl:variable name="POPrice" select="/currencies/currencyComparison[currencyNaming='Dollar' and currencyProvider='PO']/currencyRate"/>

                <xsl:variable name="threshold" select="0"/>


                <xsl:variable name="numbers" select="/currencies/currencyComparison[currencyNaming='Dollar']/currencyRate" />
                <xsl:variable name="minNumber" select="$numbers[not (. &gt; $numbers)]" />

                <xsl:variable name="No1_Value" select='format-number($amount * $No1Price, "#.000")' />
                <xsl:variable name="Lowest_Value" select='$amount * $minNumber' />
                <xsl:variable name="Lowest_Value_Name" select="/currencies/currencyComparison[currencyRate='$minNumber']/currencyProvider" />
                <xsl:variable name="saving" select='format-number($No1_Value - $Lowest_Value, "#.000")' /> 

               <xsl:choose>      
                    <xsl:when test="$saving &gt; $threshold">

                        <xsl:if test='$Lowest_Value_Name = "ThomasCook"'>
                            <p>You have gained #36;<xsl:value-of select='format-number($saving, "#.000")'/> </p>
                            <p>If you'd bought this at Thomas Cook, you would have received #36;<xsl:value-of select='format-number($Lowest_Value, "#.000")'/>! </p>
                        </xsl:if>

                        <xsl:if test='$Lowest_Value_Name = "PO"'>
                            <p>You have gained #36;<xsl:value-of select='format-number($saving, "#.000")'/> </p>
                            <p>If you'd bought this at The Post Office, you would have received #36;<xsl:value-of select='format-number($Lowest_Value, "#.000")'/>! </p>
                        </xsl:if>

                        <xsl:if test='$Lowest_Value_Name = "MS"'>
                            <p>You have gained #36;<xsl:value-of select='format-number($saving, "#.000")'/> </p>
                            <p>If you'd bought this at Marks and Spencers, you would have received #36;<xsl:value-of select='format-number($Lowest_Value, "#.000")'/>! </p>    
                        </xsl:if>

                    </xsl:when>

                    <xsl:otherwise>
                    </xsl:otherwise>

               </xsl:choose> 



        </xsl:when>

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




</xsl:template>
</xsl:stylesheet>
The problem starts in the second template match here: <xsl:template match="/currencies/currencyComparison">.

But the real issue seems to be with when I create the variables and try to use them as part of conditional logic straight after.

So say I set the variable saving here
Code:
  <xsl:variable name="saving" select='format-number($No1_Value - $Lowest_Value, "#.000")' />
And I want to use the $saving variable for conditional logic like so:

Code:
 <xsl:choose>      
    <xsl:when test="$saving &gt; $threshold">
             then do this
    </xsl:when>

    <xsl:otherwise>
    </xsl:otherwise>
  </xsl:choose>
The if tests and logic with the choose above are never executed.

Similiarly, if I try to output $saving (right after after creating the variables) in IE I get an object required error.
Code:
<xsl:value-of select="$saving">
If you view this in FF the app works fine, if you view it in IE6 or IE7 only the first template match calculations are executed.

http://dev.cubeclients.com/Local-Aff...yconvertor.htm

Any ideas where I maybe going wrong?
Thanks in advance for your help!

 
Old October 24th, 2008, 04:15 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I should mention I have just noticed something else:

This line is causing me problems in browsers:

Code:
<xsl:variable name="Lowest_Value_Name" select="/currencies/currencyComparison[currencyRate=$minNumber]/currencyProvider" />
when currencyRate=$minNumber is no in single quotes it works in FF but throws object error in IE. When currencyRate='$minNumber' has single quotes there are no errors in any browsers and the first template match calculations work in FF and IE but the secondary calculations to show what you have gained do not render.

Any ideas?

Thanks alot for your assistance!

 
Old October 24th, 2008, 01:09 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>when currencyRate=$minNumber is no in single quotes it works in FF

Sorry, I don't know what you mean by "is no in single quotes". Please show the code that doesn't work and the error message you are getting.

>When currencyRate='$minNumber' has single quotes there are no errors in any browsers

Of course not, it's perfectly valid code, just as currencyRate='70%' is perfectly valid. It tests whether the child element currencyRate contains the character string '$minNumber' - which I suspect it doesn't.


Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old October 24th, 2008, 01:45 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>2.095USD = 1 GBP

Wow, your data is out of date. 3 months is a long time on the foreign exchange markets.

>If you view this in FF the app works fine, if you view it in IE6 or IE7 only the first template match calculations are executed.

As far as I can see, I get the same results from both.

I really do think you would find it far easier to learn the language if you used a proper development environment rather than trying to develop your code directly in the browser, which tends to give you very poor error messages.



Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
assign XSLT variable to a wml variable arunagottimukkala XSLT 2 October 19th, 2007 05:15 AM
the scope of variable ccj_999 C++ Programming 9 October 26th, 2006 10:35 AM
Using variable in XSLT -Need help !!! nobitavn94 XSLT 3 October 16th, 2006 10:32 AM
Variable with application scope madhukp PHP How-To 0 January 8th, 2005 12:41 AM
Error: Intrinsic variable with Application scope danielh Classic ASP Basics 1 June 22nd, 2003 03:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.