Xpath Articles about
Xpath
Website Links For
Xpath
 

Information About

Xpath




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 XML node (the current 'context node') to another node or set of nodes. The steps are separated by "/" (i.e. path) characters. Each step has three components:

  • Axis Specifier

  • Node Test

  • Predicate


Two notations are defined, one, the abbreviated syntax, is more compact and allows XPaths to be written and read easily using intuitive and, in many cases, familiar characters and constructs. The full syntax is more verbose, but allows for more options to be specified, and is more descriptive if read carefully.


Abbreviated syntax

The compact notation allows many defaults and abbreviations for common cases. The simplest XPath 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 XML document. XPath syntax is designed to mimic URI (Uniform Resource Identifier) syntax and file path 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 any step. For example, the expression
  • A//B/--- {Link without Title}

  • '), that is a child ('/') of a B element that itself is a child or other, deeper descendant ('//') of an A element that is a child of the current context node (the expression does not begin with a '/').



Expanded syntax

In the full, unabbreviated syntax, the two examples above would be written

Here, in each step of the XPath, the axis (e.g. child or descendant-or-self) is explicitly specified, followed by :: and then the '''node test''', such as A or node() in the examples above.


AXIS SPECIFIERS

The Axis Specifier indicates navigation direction within the tree representation of the XML document. The axes available, in the full and then the abbreviated syntax, are:
;child :default, does not need specifying in abbreviated syntax
;attribute :@
;descendant :not available in abbreviated syntax
;descendant-or-self ://
;parent :.. i.e. dot-dot
;ancestor :not available in abbreviated syntax
;ancestor-or-self :not available in abbreviated syntax
;following :not available in abbreviated syntax
;preceding :not available in abbreviated syntax
;following-sibling :not available in abbreviated syntax
;preceding-sibling :not available in abbreviated syntax
;self :. i.e. dot
;namespace :not available in abbreviated syntax

As an example of using the attribute axis in abbreviated syntax, //a/@href selects an attribute called href in an a element anywhere in the document tree. The '''self''' axis is most commonly used within a predicate to refer to the currently selected node. For example, h3 also' selects an element called h3 in the current context, whose text content is See also.


NODE TESTS

Node tests may consist of specific node names or more general expressions. In the case of an XML document in which the namespace prefix gs has been defined, //gs:enquiry will find all the enquiry nodes in that namespace.

Other node test formats are:
;comment() :finds an XML comment node, e.g.
;text() :finds a node of type text, e.g. the hello in hello
;processing-instruction() :finds XML Processing Instruction s such as . In this case, processing-instruction('php') would match.
;node() :finds any node at all.


PREDICATES

Expressions of any complexity can be specified in square brackets, that must be satisfied before the preceding node will be matched by an XPath. Examples include //a {Link without Title} , which will match an a element with an href attribute whose value is help.php.

There is no limit to the number of predicates in a step, and they need not be confined to the last step in an XPath. They can also be nested to any depth. Paths specified in predicates begin at the context of the current step (i.e. that of the immediately preceding node test) and do not alter that context.

//a {Link without Title} {Link without Title} /@target will select the value of the target attribute of an a element, provided the a element has an href attribute whose value is help.php, and provided the a element has a parent div element that itself has a class attribute of value header.


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 " {Link without Title} " operators, used in path expressions, as described above.

  Inside Or Outside Of Predicates, Entire Node-sets Can Be Combined ( "http://wwwinformationdelightinfo/encyclopedia/entry/Union_(set_theory)" class="copylinks">'unioned' ) using the pipe character
  <code>v "x" class="copylinks" target="_blank">or y w[z]</code> will return a single node-set consisting of all the <code>v</code> elements that have <code>x</code> or <code>y</code> child-elements, as well as all the <code>w</code> elements that have <code>z</code> child-elements, that were found in the current context