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.
XPath (XML Path Language) is an expression language designed to support the query or transformation of XML documents. It was defined by the World Wide Web Consortium (W3C) and can be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document.
!= path-expression value is not equal to literal . A string or number used to compare against the path-expression node or attribute value. Enclose the string in single or double quotation marks.
Answer. There is no direct way to compare the dates in XSLT, but the number function we can convert the date into a number and then can perform the comparison in an 'IF' condition. The following function helps to convert the date into number and then can use the >= operator to check the date in an 'IF' condition.
In XPath 1.0, string comparison is defined only for =
and !=
, and ordering comparisons are not available. The spec 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.
Thus 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With