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 21st, 2008, 10:08 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default > doesn't seem to work in xslt

I have the following code which should result in the word 'true' being output on screen but it doesn't.

Code:
    
<xsl:choose>
   <xsl:when test="number($Price1) &gt; number($Price2)">
        true
   </xsl:when>
   <xsl:otherwise>
       Price1<xsl:value-of select='$Price1'/> 
         Price2<xsl:value-of select='$Price2'/> 
   </xsl:otherwise>
</xsl:choose>
I am confused because in the 'otherwise' condition the values are


Price1 = 1.895
Price2 = 1.881

so Price1 is greater than Price2 but the code doe not work. Can anyone help me seem my rror please?


Thanks in advance for your help!

 
Old October 21st, 2008, 10:24 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>Price1 = 1.895

I don't see anything in your code that outputs the "=" sign. That leaves two possible explanations:

(a) the "=" sign is part of the data. In that case number() will return NaN, and NaN > NaN is false, which would explain your output.

(b) the code or the output isn't exactly as you've shown it. In which case you may have accidentally changed it in a way that destroys the evidence. (For example, if you added an "=" sign, perhaps you removed a "$" sign at the same time??)

I'd suggest trying to boil it down to a complex executable source document and stylesheet that shows the problem, and that other people can run. Often in doing this you end up finding the error in your code anyway.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old October 21st, 2008, 10:28 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, my bad, I was trying to make it clear and just confused the issue:

Code:
<xsl:choose>
   <xsl:when test="number($Price1) &gt; number($Price2)">
      true
   </xsl:when>
   <xsl:otherwise>
          <xsl:value-of select='$Price1'/> 
         <xsl:value-of select='$Price2'/> 
   </xsl:otherwise>
</xsl:choose>
The output is actually this:

Code:
1.8951.881
 
Old October 21st, 2008, 10:37 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oddly if I do this, the code works:

Code:
<xsl:choose>
   <xsl:when test="number($Price1) &gt; number('1.881')">
        true
   </xsl:when>
   <xsl:otherwise>
      <xsl:value-of select='$Price1'/> 
      <xsl:value-of select='$Price2'/> 
   </xsl:otherwise>
</xsl:choose>
Could there be an issue with the type casting? The code is so simple at this point I can't see how I have missed anything. The dollar symbols are all in place.

 
Old October 21st, 2008, 10:44 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Can't see what's wrong I'm afraid.

I can only suggest

(a) post a complete executable sample that shows the problem

(b) try it on another XSLT processor to see if you get the same result.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old October 21st, 2008, 10:55 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

When you say:

'try it on another XSLT processor to see if you get the same result'

What do you mean?

I have this at the heade of the document:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
and I am using sarissa for cross browser compatibility http://dev.abiss.gr/sarissa/

Is this what you mean?

 
Old October 21st, 2008, 11:02 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well if you use XSLT in the browser then at least try different browsers to see if you get different results. And tell us exactly which browser you use when you get the problem.
As for using a different XSLT processor, you could download and use Saxon or Xalan for instance and try them from the command line. Debugging XSLT in the browser is difficult.
You can find Saxon here: http://saxon.sourceforge.net/
And Xalan here: http://xml.apache.org/xalan-j/

--
  Martin Honnen
  Microsoft MVP - XML
 
Old October 21st, 2008, 11:04 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am getting an 'object required' error in IE when this code is in:

Code:
<xsl:choose>
   <xsl:when test="number($Price1) &gt; number('1.881')">
        true
   </xsl:when>
   <xsl:otherwise>
      <xsl:value-of select='$Price1'/> 
      <xsl:value-of select='$Price2'/> 
   </xsl:otherwise>
</xsl:choose>
The error pertains to a the html file that has the js includes which in turn perform the transformations so the problem could be anywhere.

I have not used the othe rprocessors before but I will give it whirl.

Could this be a variable scope issue? The code that sets up the variables is above the &gt; condition and is like so:


Code:


<xsl:variable name="Price1">
   <xsl:choose>  
      <xsl:when test="Provider='A' and Naming='Pounds'">
       <xsl:value-of select='format-number($amount * Rate, "#.000")'/> 
       </xsl:when>
       <xsl:otherwise>
       </xsl:otherwise>
   </xsl:choose> 
</xsl:variable> 

<xsl:variable name="Price2">
   <xsl:choose>  
      <xsl:when test="Provider='B' and Naming='Pounds'">
       <xsl:value-of select='format-number($amount * Rate, "#.000")'/> 
       </xsl:when>
       <xsl:otherwise>
       </xsl:otherwise>
   </xsl:choose> 
</xsl:variable> 

<xsl:choose>
   <xsl:when test="number($Price1) &gt; number('1.881')">
        true
   </xsl:when>
   <xsl:otherwise>
      <xsl:value-of select='$Price1'/> 
      <xsl:value-of select='$Price2'/> 
   </xsl:otherwise>
</xsl:choose>
 
Old October 21st, 2008, 11:06 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I generally advise that developing and testing XSLT code in the browser is a pretty poor choice - you're much better off with a development environment like XML Spy, Stylus Studio, oXygen, or Kernow, or with products like Saxon that you can run from the command line.

But since you've done it in a cross-browser way, your first step should be to check you get the same result in different browsers - IE, Mozilla, and Opera have quite different XSLT processors embedded. If you get the same result, then it's certain there's a bug in your code and not in the XSLT processor.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old October 22nd, 2008, 04:15 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks alot for your comments and advice.

With this particular problem I don't really have time to learn and use Saxon or Xalan as they seem an education in themselves, as most powerful tools do.

Once I get this issue of performing comparisions on number variables I can finish the task in hand and revist Saxon/Xalan another time when I have more time to do this. (i know this is not ideal, but I have little choice in the matter as I need to get this resolved very quickly).

I think there is an issue with casting the variables as numbers as I have tried setting the variables again like so:

Code:
<xsl:variable name="Price1">
<xsl:choose>  
   <xsl:when test="Provider='A' and Naming='Pounds'">
     <xsl:value-of select='format-number($amount * Rate, "#.000")'/> 
   </xsl:when>
   <xsl:otherwise>
   </xsl:otherwise>
</xsl:choose> 
</xsl:variable>

<xsl:variable name="Price2">
<xsl:choose>  
   <xsl:when test="Provider='B' and Naming='Pounds'">
     <xsl:value-of select='format-number($amount * Rate, "#.000")'/> 
   </xsl:when>
   <xsl:otherwise>
   </xsl:otherwise>
</xsl:choose> 
</xsl:variable>
Instead of performing the &gt; comparison I tried this:

Code:
<xsl:value-of select='format-number($Price1 * $Price2, "#.000")'/>
I get 'NaN'. Oddly, if I do either of these things:

1.
Code:
<xsl:value-of select='format-number($Price1 * 2, "#.000")'/>
2.
Code:
<xsl:value-of select='format-number($Price2 * 2, "#.000")'/>
I get a number. It is only when I try to multiply them by each other the issue starts.

Any ideas what could be causing this? I am a total xslt noob as you may have guessed. I am trying to approach this as I would PHP or ASP in this way:


Code:
if ($Price1 > $Price2){
 do this
}else{
 do the other
}
But by doing this in xslt:
Code:
<xsl:choose>  
    <xsl:when test="$Price1 &gt; $Price2">
       do this
   </xsl:when>
   <xsl:otherwise>
        do the other
   </xsl:otherwise>
</xsl:choose>
Can anyone offer some advice on what maybe going wrong? My hunch is that the variables are losing their casting when compared against each for some reason. Do I need to enforce this casting again somehow?

Many thanks!







Similar Threads
Thread Thread Starter Forum Replies Last Post
.Net2 xslt does not like the &lt; "<" - &gt; ">" ismailc XSLT 4 October 11th, 2008 04:40 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
Forms with XML > XSLT > XHTML kwilliams XSLT 0 October 27th, 2005 09:52 AM
"Microsoft&amp;#174;" --> "Microsoft®" rockon C# 1 October 20th, 2005 01:27 AM
XML from a DB recordset (removal of &lt;&gt;) Thodoris XML 3 July 13th, 2004 12:28 AM





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