Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare strings with Xpath 1.0?

I am experiiencing an issue with the < operator on strings in Xpath 1.0.

This simple Xpath expression

'A' < 'B' (or the equivalent 'A' &lt; '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.

like image 256
Maestro13 Avatar asked Jun 20 '12 18:06

Maestro13


People also ask

What is XPath string?

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.

How do you write not equal to in XML?

!= 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.

How do you compare in XSLT?

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.


1 Answers

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.

like image 130
Jim Garrison Avatar answered Sep 18 '22 23:09

Jim Garrison