Why did the definition of dot (.) change between XPath 1.0 and 2.0?
Asked Answered
B

2

7

When researching details for an answer to an XPath question here on Stack Overflow, I run into a difference between XPath 1.0 and 2.0 I can find no rationale for.

I tried to understand what . really means.

What was the rationale for the change? Is there a difference between . and self::node() in XPath 2.0?

From the spec itself, the intent of the change is not clear to me. I tried googling keywords like dot or period, primary expression, and rationale.

Bibliopegy answered 2/9, 2016 at 19:57 Comment(0)
M
5

XPath 1.0 had four data types: string, number, boolean, and node-set. There was no way of handling collections of values other than nodes. This meant, for example, that there was no way of summing over derived values (if elements had attributes of the form price='$23.95', there was no way of summing over the numbers obtained by stripping off the $ sign because the result of such stripping would be a set of numbers, and there was no such data type).

So XPath 2.0 introduced more general sequences, and that meant that the facilities for manipulating sequences had to be generalised; for example if $X is a sequence of numbers, then $X[. > 0] filters the sequence to include only the positive numbers. But that only works if "." can refer to a number as well as to a node.

Maddeu answered 3/9, 2016 at 7:8 Comment(0)
B
4

In short: self::node() filters out atomic items, while . does not. Atomic items (numbers, strings, and many other XML Schema types) are not nodes (unlike elements, attributes, comments, etc.).

Consider the example from the spec: (1 to 100)[. mod 5 eq 0]. If the . is replaced by self::node(), the expression is not valid XPath, because mod requires both arguments to be numeric and atomization does not help in this case.

For those scanning the spec: XPath 2.0 defines item() type-matching construct, but it has nothing to do with node tests as atomics are not nodes and axis steps always return just nodes. Therefore, dot cannot be defined as self::item(). It really needs to be a special language construct.

Bibliopegy answered 2/9, 2016 at 19:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.