RE: [xsl] comparing a string to a number?

Subject: RE: [xsl] comparing a string to a number?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 17 Sep 2003 09:37:48 -0400
At 2003-09-17 09:16 -0400, cknell@xxxxxxxxxx wrote:
Yes, '1A' will always be "less than" '20' when compared as strings. String sorting is based on the numeric value (usually ASCII) of each successive character. Thus '10000000' will be "less than" '2'.

I disagree .... in XPath 1.0 the "less than" infers numeric data typing on the operands, and does not preserve the string typing.


Ref XPath 1.0 Section 3.4 paragraph starting with:

===8<---
When neither object to be compared is a node-set and the operator is <=, <, >= or >, then the objects are compared by converting both objects to numbers and comparing the numbers according to IEEE 754. The < comparison will be true if and only if the first number is less than the second number.
===8<---


Therefore ( '100000000' < '2' ) in XPath is *always* false because 1000000 is greater than 2.

-----Original Message-----
From:     "Keller, Matt" <Keller.Matt@xxxxxxxxxxxxx>
Sent:     Tue, 16 Sep 2003 17:18:19 -0500
...
<xsl:if test="/Output/response/Field < '20'">

This is confusing to me for a few reasons.

1) The '20' is in quotes
2) the value of the Field node is most often a number, but on some occasions it's a letter/number pair, like 1A or 1B.


Can this check ever work? Would 1A ever be "less" than '20'?

Since the numeric data type inference would change "1A" to NaN (not-a-number), and NaN doesn't compare true in numeric terms to anything (including itself), the value of ( "1A" < '20 ) will *always* be false.


I hope this helps.

.................. Ken


T:\ftemp>type knell.xsl <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:template match="/">
  <xsl:value-of select="'10000000' &lt; '2'"/>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>saxon knell.xsl knell.xsl
<?xml version="1.0" encoding="utf-8"?>false
T:\ftemp>







--
Next public European delivery:  3-day XSLT/2-day XSL-FO 2003-09-22
Next public US delivery:        3-day XSLT/2-day XSL-FO 2003-10-13
Instructor-led on-site corporate, government & user group training
for XSLT and XSL-FO world-wide:  please contact us for the details

G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
ISBN 0-13-065196-6                       Definitive XSLT and XPath
ISBN 0-13-140374-5                               Definitive XSL-FO
ISBN 1-894049-08-X   Practical Transformation Using XSLT and XPath
ISBN 1-894049-11-X               Practical Formatting Using XSL-FO
Member of the XML Guild of Practitioners:     http://XMLGuild.info
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread