XPath

From Exampleproblems

Jump to: navigation, search

XPath (XML Path Language) is a terse (non-XML) syntax for addressing portions of an XML document.

Originally motivated by a desire to provide a common syntax and behavior model between XPointer and XSL, XPath has rapidly been adopted by developers as a small query language.

Notation

The most common kind of XPath expression (and the one which gave the language its name) is a path expression. A path expression is written as a sequence of steps to get from one set of nodes to another set of nodes. The steps are separated by "/" (i.e. path) characters. Each step has three components:

  • Axis Specifier
  • Node Test
  • Predicate

The notation is compact, allowing many defaults and abbreviations for common cases. The simplest kind of path expression takes a form such as /A/B/C, which selects C elements that are children of B elements that are children of the A element that forms the outermost element of the document. XPath syntax is designed to mimic URI (Uniform Resource Identifier) syntax or file name syntax. More complex expressions can be constructed by specifying an axis other than the default child axis, a node test, other than a simple name, or predicates, which can be written in square brackets after each step. For example, the expression /A/B/following-sibling::*[1] selects all elements (whatever their name) that immediately follow a B element that is a child of the outermost A element.

The Axis Specifier indicates navigation direction within the tree representation of the XML document. The axes available are: child, attribute, descendant, descendant-or-self, parent, ancestor, ancestor-or-self, following, preceding, following-sibling, preceding-sibling, self, and namespace. The attribute axis can be selected using the abbreviation "@". The descendant axis can be invoked by writing "//" instead of "/": thus //@id selects all the attributes, named id, anywhere in the document.

As well as path expressions, XPath also allows more conventional expressions, involving arithmetic and boolean operators and a range of functions to perform string manipulation, aggregation and similar operations. These expressions are usually encountered within a predicate of a path expression. Thus the expression //item[@price > 2*@discount] selects items whose price attribute is at least twice the numeric value of the discount attribute.

Functions and operators

XPath 1.0 defines four data types: node-sets (sets of nodes with no intrinsic order), strings, numbers and booleans.

The available operators are:

  • The "/", "//" and "[...]" operators, used in path expressions, as described above.
  • A union operator, "|", which forms the union of two node-sets.
  • Boolean operators "and" and "or", and a function "not()"
  • Arithmetic operators "+", "-", "*", "div" (divide), and "mod"
  • Comparison operators "=", "!=", "<", ">", "<=", ">="

The function library includes:

  • Functions to manipulate strings: concat(), substring(), contains(), substring-before(), substring-after(), translate(), normalize-space(), string-length()
  • Functions to manipulate numbers: sum(), round(), floor(), ceiling()
  • Functions to get properties of nodes: name(), local-name(), namespace-uri()
  • Functions to get information about the processing context: position(), last()
  • Type conversion functions: string(), number(), boolean()

Examples

XPath expressions which might be used in an XSL context are:

  • The Root element: /*
  • All elements everywhere (implementations of this expression can be very slow): //*
  • All Top Level Elements (children of Root): /*/*
  • The fifth child element (of the current node, as defined by the context of the expression) named "FOOB": FOOB[5]
  • All child elements (of the current node, as defined by the context of the expression) FOOB whose BAZ attributes are "untrue": FOOB[ @BAZ = "untrue" ]

XPath 2.0

XPath 1.0 was published as a W3C Recommendation on 16 November 1999. XPath 2.0 is in the final stages of the W3C approval process. XPath 2.0 represents a significant increase in the size and capability of the XPath language.

The most notable change is that XPath 2.0 has a much richer type system; XPath 2.0 supports atomic types, defined as built-in types in XML Schema, and may also import user-defined types from a schema. Every value is now a sequence (a single atomic value or node is regarded as a sequence of length one). XPath 1.0 node-sets are replaced by node sequences, which may be in any order.

To support richer type sets, XPath 2.0 offers a greatly-expanded set of functions and operators.

XPath 2.0 is in fact a subset of XQuery 1.0. It offers a for expression which is a cut-down version of the "FLWOR" expressions in XQuery. It is possible to describe the language by listing the parts of XQuery that it leaves out: the main examples are the query prolog, element and attribute constructors, the remainder of the "FLWOR" syntax, and the typeswitch expression.

See also

External links

de:XPath es:XPath fr:XPath he:XPath nl:XPath pl:XPath ru:XPath sv:XPath zh:XPath

Argan Oil
Natural Skin Care
Organic Skin Care
visitor stats