ArticleslgStudy

science

XPath 2.0

XPath 2.0 is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand XPath 2.0 rather than just read about it. In short: XPath 2.0 is a version of the XPath language defined by the World Wide Web Consortium, W3C. It became a recommendation on 23 January 2007.

XPath 2.0 — main illustration
XPath 2.0 — illustration

Key takeaways

  • XPath 2.0 belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect XPath 2.0 to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of XPath 2.0 from memory before moving on to harder problems.

Reference excerpt

XPath 2.0 is a version of the XPath language defined by the World Wide Web Consortium, W3C. It became a recommendation on 23 January 2007. As a W3C Recommendation it was superseded by XPath 3.0 on 10 April 2014. XPath is used primarily for selecting parts of an XML document. For this purpose the XML document is modelled as a tree of nodes. XPath allows nodes to be selected by means of a hierarchic navigation path through the document tree. The language is significantly larger than its predecessor, XPath 1.0, and some of the basic concepts such as the data model and type system have changed. The two language versions are therefore described in separate articles. XPath 2.0 is used as a sublanguage of XSLT 2.0, and it is also a subset of XQuery 1.0. All three languages share the same data model (the XDM), type system, and function library, and were developed together and published on the same day.

Data model

Every value in XPath 2.0 is a sequence of items. The items may be nodes or atomic values. An individual node or atomic value is considered to be a sequence of length one. Sequences may not be nested. Nodes are of seven kinds, corresponding to different constructs in the syntax of XML: elements, attributes, text nodes, comments, processing instructions, namespace nodes, and document nodes. (The document node replaces the root node of XPath 1.0, because the XPath 2.0 model allows trees to be rooted at other kinds of node, notably elements.) Nodes may be typed or untyped. A node acquires a type as a result of validation against an XML Schema. If an element or attribute is successfully validated against a particular complex type or simple type defined in a schema, the name of that type is attached as an annotation to the node, and determines the outcome of operations applied to that node: for example, when sorting, nodes that are annotated as integers will be sorted as integers. Atomic values may belong to any of the 19 primitive types defined in the XML Schema specification (for example, string, boolean, double, float, decimal, dateTime, QName, and so on). They may also belong to a type derived from one of these primitive types: either a built-in derived type such as integer or Name, or a user-defined derived type defined in a user-written schema.

Type system

The type system of XPath 2.0 is noteworthy for the fact that it mixes strong typing and weak typing within a single language. Operations such as arithmetic and boolean comparison require atomic values as their operands. If an operand returns a node (for example, @price * 1.2), then the node is automatically atomized to extract the atomic value. If the input document has been validated against a schema, then the node will typically have a type annotation, and this determines the type of the resulting atomic value (in this example, the price attribute might have the type decimal). If no schema is in use, the node will be untyped, and the type of the resulting atomic value will be untypedAtomic. Typed atomic values are checked to ensure that they have an appropriate type for the context where they are used: for example, it is not possible to multiply a date by a number. Untyped atomic values, by contrast, follow a weak typing discipline: they are automatically converted to a type appropriate to the operation where they are used: for example with an arithmetic operation an untyped atomic value is converted to the type double.

Path expressions The location paths of XPath 1.0 are referred to in XPath 2.0 as path expressions. Informally, a path expression is a sequence of steps separated by the "/" operator, for example a/b/c (which is short for child::a/child::b/child::c). More formally, however, "/" is simply a binary operator that applies the expression on its right-hand side to each item in turn selected by the expression on the left hand side. So in this example, the expression a selects all the element children of the context node that are named <a>; the expression child::b is then applied to each of these nodes, selecting all the <b> children of the <a> elements; and the expression child::c is then applied to each node in this sequence, which selects all the <c> children of these <b> elements. The "/" operator is generalized in XPath 2.0 to allow any kind of expression to be used as an operand: in XPath 1.0, the right-hand side was always an axis step. For example, a function call can be used on the right-hand side. The typing rules for the operator require that the result of the first operand is a sequence of nodes. The right hand operand can return either nodes or atomic values (but not a mixture). If the result consists of nodes, then duplicates are eliminated and the nodes are returned in document order, an ordering defined in terms of the relative positions of the nodes in the original XML tree. In many cases the operands of "/" will be axis steps: these are largely unchanged from XPath 1.0, and are described in the article on XPath 1.0.

Other operators Other operators available in XPath 2.0 include the following:

Conditional expressions may be written using the syntax if (A) then B else C. XPath 2.0 also offers a for expression, which is a small subset of the FLWOR expression from XQuery. The expression for $x in X return Y evaluates the expression Y for each value in the result of expression X in turn, referring to that value using the variable reference $x.

Function library The function library in XPath 2.0 is greatly extended from the function library in XPath 1.0. (Bold items are available in XPath 1.0) The functions available include the following:

Backwards compatibility Because of the changes in the data model and type system, not all expressions have exactly the same effect in XPath 2.0 as in 1.0. The main difference is that XPath 1.0 was more relaxed about type conversion, for example comparing two strings ("4" > "4.0") was quite possible but would do a numeric comparison; in XPath 2.0 this is defined to compare the two values as strings using a context-defined collating sequence. To ease transition, XPath 2.0 defines a mode of execution in which the semantics are modified to be as close as possible to XPath 1.0 behavior. When using XSLT 2.0, this mode is activated by setting version="1.0" as an attribute on the xsl:stylesheet element. This still doesn't offer 100% compatibility, but any remaining differences are only likely to be encountered in unusual cases.

Support

Support for XPath 2.0 is still limited.

References

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with XPath 2.0

Start with the simplest possible case. Write down what XPath 2.0 claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to XPath 2.0 before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about XPath 2.0 ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of XPath 2.0

In research
XPath 2.0 appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses XPath 2.0 in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
XPath 2.0 is common in secondary-school and first-year university syllabi. It links to neighbouring topics XML data access, so understanding it makes those chapters shorter.
In everyday life
Look for XPath 2.0 outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “XPath 2.0” →

How to study XPath 2.0 in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what XPath 2.0 means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain XPath 2.0 out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is XPath 2.0 in simple terms?

XPath 2.0 is a version of the XPath language defined by the World Wide Web Consortium, W3C. It became a recommendation on 23 January 2007.

Why does XPath 2.0 matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study XPath 2.0?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on XPath 2.0.

Tags

  • XML data access

Keep exploring