I have following question relating to XPATH1 and XPATH2:
<document>
<val>3</val>
<val>11</val>
<val>3</val>
<val>2</val>
<val>12</val>
<val>5</val>
<val>0</val>
<val>7</val>
</document>
xpath1: max
//val[not(. < //val)]
min:
//val[not(. > //val)]
xpath2:
document/val[. = max(//val)]
document/val[. = min(//val)]
Why don’t I get the same result in XPATH2 processing with the xpath1-max query:
//val[not(. < //val)]
It seems that I get the LAST node (with val 7), but not the highest value … the other way around for MIN value works fine with XPATH2 processing:
//val[not(. > //val)]
Can someone help me out there ?
Very good point.
In xpath 1 (and xslt 1.0),
//val[not(. < //val)]is inherently casting the text values to numbers, and doing the comparison on numbers.Hence, you get the correct min and max values.
However, in xpath 2 (and xslt 2.0), the text isn’t cast to numeric, and instead, a string comparison is done. So using string comparison, ‘7’ is > ’12’ and hence 7 will still be the highest value (it isn’t returning just the last value in the list – try swapping the order of values around)
So what you should do to be safe in both xslt 1 and 2 is to use the number() function to do the cast on the text, to ensure that a numeric comparison is done.
Max:
Min:
Using Saxon with the stylesheet:
Returns
as expected.