Dates as strings - using comparions in XPath

D

dingbat

Losing my marbles here (some sleep would help!)

Why are the following XPath comparisons both returning false ?

<xsl:value-of select="string('2005-09-22T12:43:23') &gt;
string('2005-09-15T14:06:58')" />

<xsl:value-of select="string('2005-09-15T14:06:58') &gt;
string('2005-09-22T12:43:23')" />

Shouldn't these just act as simple string comparisons and behave
themselves? As the dates are ymd and 0 padded, then this is also
adequate for a date comparison?

I keep staring at the code, but it doesn't get any clearer 8-(


Thanks!
 
M

Martin Honnen

Losing my marbles here (some sleep would help!)

Why are the following XPath comparisons both returning false ?

<xsl:value-of select="string('2005-09-22T12:43:23') &gt;
string('2005-09-15T14:06:58')" />

<xsl:value-of select="string('2005-09-15T14:06:58') &gt;
string('2005-09-22T12:43:23')" />

Shouldn't these just act as simple string comparisons and behave
themselves? As the dates are ymd and 0 padded, then this is also
adequate for a date comparison?


For primitive values the <, >, <=, and >= operators in XPath 1.0 are
defined on numbers only so any operands are converted to numbers and
those are compared. In those examples you compare
NaN > NaN
and that yields false (NaN is the "number" value you get for anything
that can not be converted to a number).

See <http://www.w3.org/TR/xpath#booleans> where it says:
"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."


So you can try as hard as you want, XPath 1.0 does not do < or >
comparisons on strings.

As for those date/time strings I think you could try
translate('2005-09-15T14:06:58', '-T:', '') <
translate('2005-09-22T12:43:23', '-T:', '')
that would then yield numbers which could be compared.
 
D

dingbat

Martin said:
In those examples you compare
NaN > NaN
and that yields false

Thanks - I'd guessed that NaN was showing up somewhere, as both were
returning the same value (the old "propagating nulls" problem in
database-speak). I could have sworn XPath comparisons worked on strings
though!

I've re-written it to use <xsl:for-each><xsl:sort> and just return the
value of the first item.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,000
Messages
2,570,252
Members
46,848
Latest member
CristineKo

Latest Threads

Top