I am experiiencing an issue with the <
operator on strings in Xpath 1.0.
This simple Xpath expression
'A' < 'B' (or the equivalent 'A' < 'B')
did not evaluate to true in my xslt run in libxslt (which is an XSLT 1.0 engine).
I checked in XML Spy, which allows testing Xpath expressions in both 1.0 and 2.0, and sure enough, in Xpath 2.0 it evaluates to true
, but in Xpath 1.0 it evaluates to false
!
Is this a bug in Xpath 1.0?
What other expression should I use to compare two strings/characters for their alphabetical order? Note that the compare() function will not do, as this is an XSLT 2.0 function.
It might be ugly solution, and not feasible in many situations, but for simple alphabetical order comparison you can use
translate
. The following snippet is just an example that can be extended furtherly:Your translate expression should cover all letters, up and low cases, and could be conveniently reused by defining a named template.
Yes, this is a limitation of XPath 1.0. (I don't think it's reasonable to refer to a limitation you don't like as a "bug", though clearly the designers of XPath 2.0 agreed with you that it was an undesirable limitation).
You've tagged your question "xslt", so you may be able to work around the problem at the XSLT level, at least if your processor has the node-set extension:
But perhaps it's time to move to 2.0. What's holding you back?
In XPath 1.0, string comparison is defined only for
=
and!=
, and ordering comparisons are not available. The spec saysThus both your operands are being converted to float, making them both NaN.
I believe Microsoft's XML adds extension functions to handle this, but of course this helps only if you're using MSXML.
In the hope that this proves to be useful to others too, below is the code I wrote following Michael Kay's suggestion. I wrote a custom
compare
function that gives the same results as Xpath 2.0's one. I also added thephp
tag to the question so that it will be found more often.The result of running this (with dummy input) is
For those who wish to test this in php for themselves, here's the code I used: