The XML Path Language (XPath ) is a technology developed by the W3 consortium query language for addressing parts of an XML document and evaluate. XPath is used as the basis of a number of other standards such as XSLT, XPointer, and XQuery. Since its adoption on 23 January 2007 XPath 2.0 has the force since 1999 Revision replaced XPath 1.0. XPath 2.0 has 1.0 redefine some concepts of XPath and also has a much wider range of functions, such as regular expression support for strings.
An XPath expression addressing parts of an XML document that is viewed as a tree, with some differences from the "classical" tree of the graph theory should be observed:
- Nodes ( nodes ) of the tree represent the document node, XML elements, attributes, text nodes, comments, namespaces, and processing instructions.
- The axes Preceding, following, preceding -sibling and following-sibling based not only on the tree, but also on the declaration order of the elements in the XML document ( linked- Tree ).
An XPath expression consists of one or more location steps ( Location Steps). They are separated by the character " /".
A location step axis :: node- test [ predicate 1] [ 2 predicate ] ... consists of:
- Axis (axis) and
- Test node (node- test),
- Optionally followed by one or more predicates ( predicates ).
Any number of XPath expressions can be personalized with the pipe character | unite quantified.
There are always different ways to express a requested node set in XPath.
XPath operates on the logical document structure. This means, for example, one finds that entities already parsed or that any standard attributes and nodes that are specified by a schema, are already included in the tree.
By specifying axes is starting to navigate the current context node in the tree structure of the XML document.
This approach assumes the document node ( the root of the XML document ), the XPath expression is the / character prepended.
This tree visualization by way of example the structure of an XML document,
Document node | A _ | _ | | B L __ | ________ | | | | C (D ) H I _ | __ | _ | | | | E G J K | F The five axes self, ancestor, descendant, preceding and Following form starting from any node of the document tree completely and without overlap from.
Node tests (written axis :: node test ) restrict the element selection one axis:
- Specifying an element name selects all corresponding elements. Example: / descendant-or- self :: Foo selects all elements in the document that have the name " foo".
- With the * character to choose arbitrary elements. Example: / descendant-or- self :: foo / child :: * selects all elements in the document, the children of elements with the name " foo" are.
- With text (), comment () and processing-instruction () node is a specific type can be choose.
By specifying predicates, the result can be further restricted. Predicates are enclosed in square brackets and can be in any number consecutively written, the sequence is essential. Predicates can XPath expressions contain, in addition, a variety of functions and operators are used. Are for example:
- Access index (counting starts with 1)
- Relation symbols: = and or = <> < => =!
- String functions: normalize-space () - remove spaces at the beginning and end of the string and reduction of consecutive spaces to one
- Substring () - select a substring
- Select a substring before the first occurrence of the delimiter - substring-before (source, splitter )
- Substring-after (source, splitter ) - select a substring after the first occurrence of the delimiter
- String-length () - length of the string
- Count () - number of nodes in a node set
- Id () - Selects elements of the DTD ID
- Name () - name of the node
- / / child :: book / chapter all chapters of all books.
- / / child :: book / Chapter All the first chapter of any book.
- / / child :: book [count (. / page) <= 100 ] [count (. / page) > = 10 ] returns all nodes of type " book", but have at least 10 more than 100 child elements of type "Page ".
(the same makes / / book [count (page ) <= 100 and count ( page ) > = 10 ] )
- Substring-before ( $ variable, ':') selects the substring before the first colon in the value of the variable named variable
Given the following XML document:
xml version = " 1.0" encoding = "utf -8" standalone = "yes"? >
Help XPath Visualizer to apply the sometimes complex XPath queries to specific XML files.