Differences Between XQuery 1.0/XPath 2.0 and XPath 1.0

XPath 1.0 is a subset of XPath 2.0, which is essentially a subset of XQuery 1.0. If you already know XPath 1.0 from using it in XSLT 1.0, you will probably find parts of XQuery very familiar.

Backward- and cross-compatibility are mostly maintained among the three languages, so that an expression in any of the three languages will usually yield the same results. However, there are a few important differences, which are described in this section. All of these differences between XPath 1.0 and XPath 2.0 are also relevant if you plan to use XSLT 2.0.

The few areas of backward incompatibility between XPath 1.0 and XPath 2.0 are discussed in greater detail in Appendix I of the XPath 2.0 specification, which is at http://www.w3.org/TR/xpath20. In XSLT, you can choose to process 2.0 stylesheets while setting an XPath 1.0 Compatibility Mode to treat XPath expressions just like XPath 1.0 expressions. This helps to avoid unexpected changes in the behavior of stylesheets when they are upgraded from 1.0 to 2.0. The mode is not available in XQuery 1.0, since there is no previous version of XQuery.

Data Model

The XPath 1.0 data model has the concept of a node-set, which is a set of nodes that are always in document order. In XQuery 1.0/XPath 2.0, there is the similar concept of a sequence. However, sequences differ in that they are ordered (not necessarily in document order), and they can contain duplicates. Another difference is that sequences can contain atomic values as well as nodes.

Root nodes in XPath 1.0 have been renamed document nodes in XQuery 1.0/XPath 2.0. Namespaces nodes are now deprecated in XPath 2.0, and not at all accessible in XQuery 1.0. They have been replaced by two functions that provide information about the namespaces in scope: in-scope-prefixes and namespace-uri-for-prefix.

New Expressions

XPath 2.0 encompasses a lot more than just paths. Some of the new kinds of expressions include:

  • Conditional expressions (if-then-else)

  • for expressions, which are a subset of XQuery FLWORs that have only two clauses: one for and one return

  • Quantified expressions (some/every-satisfies)

  • Ordered sequence constructors (($x, $y))

  • Additional operators to combine sequences (intersect, except)

  • Node comparison operators (is, <<, >>)

These new expressions are all part of XPath 2.0 itself, not just XQuery.

Path Expressions

If you already use XPath 1.0, the path expressions in XQuery should be familiar. The basic syntax and meaning of node tests and predicates is the same. The set of axes is almost the same, except that the namespace:: axis is not available.

There are some additional enhancements to path expressions. One is the ability to have the last step in a path return atomic values rather than nodes. So, for example,

doc("catalog.xml")//product/name/substring(.,1,5)

will return the first five characters of each product name, resulting in a sequence of four string atomic values. This makes it really easy to do things that were tough in XPath 1.0; for example, summing over the product of price and quantity becomes:

 sum(//item/(@price * @qty))

Another improvement is that it is now possible to have any expression as a step. You can take advantage of all the newly added kinds of expressions described in the previous section. It also allows you to create navigational functions that are very useful as steps, for example:

customer/orders-for-customer(.)/product-code

Function Conversion Rules

In XPath 1.0, if you call a function that is expecting a single value, and pass it a sequence of multiple values, it simply takes the first value and discards the others. For example:

substring(doc("catalog.xml")//product/name,1,5)

In this case, there are four product nodes. XPath 1.0 just takes the name of the first one and returns Fleec. In XQuery 1.0/XPath 2.0, a type error is raised.

XQuery 1.0/XPath 2.0 is strongly typed, while XPath 1.0 is not. In XPath 1.0, if you pass a value to a function that is of a different type—for example, a number to a function expecting a string, or vice versa—the value is cast automatically. In XQuery 1.0/XPath 2.0, a type error is raised. For example:

substring(12345678,1,4)

attempts to take the substring of a number. It will return 1234 in XPath 1.0, and raise an error in XQuery 1.0/XPath 2.0. Instead, you would need to explicitly convert the value into a string, as in:

substring(string(12345678),1,4)

Arithmetic and Comparison Expressions

In XPath 1.0, performing an arithmetic operation on a "missing" value results in the value NaN. In XQuery 1.0/XPath 2.0, it returns the empty sequence—for example, the expression:

catalog/foo * 2

Similar to the function conversion rules, in XPath 1.0 an arithmetic expression will take the first value of a sequence and discard the rest. In XQuery 1.0/XPath 2.0, it raises a type error.

It is possible in XQuery 1.0/XPath 2.0 to compare non-numeric values such as strings using the operators <, <=, >, and >=. In XPath 1.0, this was not supported. By default, XQuery 1.0/XPath 2.0 treats untyped operands of a comparison like strings, whereas they were treated as numbers in XPath 1.0. This is a significant compatibility risk, because the results of the comparison will be different if, for example, you are comparing <price>29.99</price> to <price>100.00</price>. XPath 1.0 would say that the first price is less than the second, while XQuery 1.0/XPath 2.0 (in the absence of a schema that says they are numeric) would say that the second price is less, because its string value is lower.

Built-in Functions

There are approximately 80 new built-in functions in XPath 2.0. All of the built-in functions from XPath 1.0 are also supported in XQuery 1.0/XPath 2.0, with a couple of minor differences:

  • Some XQuery 1.0/XPath 2.0 function calls return the empty sequence where in XPath 1.0 they would have returned a zero-length string. This is the case if the empty sequence is passed as the first argument to substring, substring-before, or substring-after.

  • Some XQuery 1.0/XPath 2.0 function calls return the empty sequence where in XPath 1.0 they would have returned the value NaN. This is the case if the empty sequence is passed to round, floor, or ceiling.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.47.166