|
Article on other languages:
|
XPath (XML Path Language) is a language for selecting nodes from an XML document. In addition, XPath may be used to compute values (strings, numbers, or boolean values) from the content of an XML document. The current version of the language is XPath 2.0, but version 1.0 is still more widely used. The XPath language is based on a tree representation of the XML document, and provides the ability to navigate around the tree, selecting nodes by a variety of criteria. In popular use (though not in the official specification), an XPath expression is often referred to simply as an XPath. Originally motivated by a desire to provide a common syntax and behavior model between XPointer and XSLT, subsets of the XPath query language are used in other W3C specifications such as XML Schema and XForms.
Syntax and SemanticsThe most important kind of expression in XPath is a location path. A location path consists of a sequence of location steps. Each location step has three components:
An XPath expression is evaluated with respect to a context node. An Axis Specifier such as 'child' or 'descendant' specifies the direction to navigate from the context node. The node test and the predicate are used to filter the nodes specified by the axis specifier: For example the node test 'A' requires that all nodes navigated to must have label 'A'. A predicate can be used to specify that the selected nodes have certain properties, which are specified by XPath expressions themselves. Two notations are defined; the first, known as 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 syntaxThe compact notation allows many defaults and abbreviations for common cases. Given source XML containing at least
the simplest XPath takes a form such as
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
selects the first element (' Expanded syntaxIn the full, unabbreviated syntax, the two examples above would be written
Here, in each step of the XPath, the axis (e.g. Axis specifiersThe Axis Specifier indicates navigation direction within the tree representation of the XML document. The axes available are:
As an example of using the attribute axis in abbreviated syntax, Node testsNode tests may consist of specific node names or more general expressions. In the case of an XML document in which the namespace prefix Other node test formats are:
PredicatesExpressions of any complexity can be specified in square brackets, that must be satisfied before the preceding node will be matched by an XPath. For example 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. All predicates must be satisfied for a match to occur. The expression Predicate order is significant, however. Each predicate 'filters' a location step's selected node-set in turn. So Functions and operatorsXPath 1.0 defines four data types: node-sets (sets of nodes with no intrinsic order), strings, numbers and booleans. The available operators are:
The function library includes:
Some of the more commonly useful functions are detailed below. For a complete description, see the W3C Recommendation document Node set functions
String functions
Boolean functions
Number functions
Usage examplesExpressions can be created inside predicates using the operators:
Entire node-sets can be combined ('unioned') using the pipe character |. Node sets that meet one or more of several conditions can be found by combining the conditions inside a predicate with '
ExamplesGiven a sample XML document
<?xml version="1.0" encoding="utf-8"?>
<wikimedia>
<projects>
<project name="Wikipedia" launch="2001-01-05">
<editions>
<edition language="English">en.wikipedia.org</edition>
<edition language="German">de.wikipedia.org</edition>
<edition language="French">fr.wikipedia.org</edition>
<edition language="Polish">pl.wikipedia.org</edition>
<edition language="Spanish">es.wikipedia.org</edition>
</editions>
</project>
<project name="Wiktionary" launch="2002-12-12">
<editions>
<edition language="English">en.wiktionary.org</edition>
<edition language="French">fr.wiktionary.org</edition>
<edition language="Vietnamese">vi.wiktionary.org</edition>
<edition language="Turkish">tr.wiktionary.org</edition>
<edition language="Spanish">es.wiktionary.org</edition>
</editions>
</project>
</projects>
</wikimedia>
The XPath expression /wikimedia/projects/project/@name Selects name attributes for all projects, and /wikimedia//editions Selects all editions of all projects, and /wikimedia/projects/project/editions/edition[@language="English"]/text() Selects addresses of all English Wikimedia projects (text of all edition elements where language attribute is equal to English), and the following /wikimedia/projects/project[@name="Wikipedia"]/editions/edition/text() Selects addresses of all Wikipedias (text of all edition elements that exist under project element with a name attribute of Wikipedia) Implementations
The Java package
Use of XPath in Schema LanguagesXPath is increasingly used to express constraints in schema languages for XML.
See also
External links
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.
Mercedes Car
This site monitored by SitePinger.net