Information AboutXpath |
| CATEGORIES ABOUT XPATH | |
| w3c standards | |
| xml data access | |
|
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 , XPath has rapidly been adopted by developers as a small Query Language , and subsets are used in other W3C specifications such as XML Schema and XForms . 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:
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 syntax The compact notation allows many defaults and abbreviations for common cases. 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
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 : // ;descendant-or-self :not available in abbreviated syntax ;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
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 ;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. For example //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. All predicates must be satisfied for a match to occur. When //a {Link without Title} {Link without Title} {Link without Title} /@target is applied to a XHTML document, it selects the value of the target attribute of the first a element that has its href attribute set to help.php, provided the document's html top-level element also has a lang attribute set to en. The reference to an attribute of the top-level element in the first predicate affects neither the context of other predicates nor that of the location step itself.Predicate order is significant, however. Each predicate 'filters' a location step's selected node-set in turn. //a {Link without Title} {Link without Title} {Link without Title} /@target will find a match only if the ''first'' a element in a @lang='en' document also meets @href='help.php'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:
  |
Inside Or Outside Of Predicates, Entire Node-sets Can Be Combined (
| "http://wwwinformationdelightinfo/information/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 |
|
|
|