YouTip LogoYouTip

Xpath Syntax

* * * XPath uses path expressions to select nodes or node sets in an XML document. Nodes are selected by following a path or steps. * * * ## XML Sample Document We will use this XML document in the examples below. ## Example Harry Potter29.99Learning XML39.95 * * * ## Selecting Nodes XPath uses path expressions to select nodes in an XML document. Nodes are selected by following a path or steps. The most useful path expressions are listed below: | Expression | Description | | --- | --- | | nodename | Selects all child nodes of this node. | | / | Selects from the root node (selects children). | | // | Selects nodes in the document from the current node that match the selection, regardless of their position (selects descendants). | | . | Selects the current node. | | .. | Selects the parent node of the current node. | | @ | Selects attributes. | In the table below, we have listed some path expressions along with their results: | Path Expression | Result | | --- | --- | | bookstore | Selects all nodes named bookstore. | | /bookstore | Selects the root element bookstore. Note: If the path starts with a forward slash (/), this path always represents an absolute path to an element! | | bookstore/book | Selects all book elements that are children of bookstore. | | //book | Selects all book elements, regardless of their position in the document. | | bookstore//book | Selects all book elements that are descendants of the bookstore element, regardless of their position under bookstore. | | //@lang | Selects all attributes named lang. | * * * ## Predicates Predicates are used to find a specific node or a node containing a specified value. Predicates are enclosed in square brackets. In the table below, we list some path expressions with predicates and their results: | Path Expression | Result | | --- | --- | | /bookstore/book | Selects the first book element that is a child of bookstore. | | /bookstore/book[last()] | Selects the last book element that is a child of bookstore. | | /bookstore/book[last()-1] | Selects the second-to-last book element that is a child of bookstore. | | /bookstore/book[position()35.00] | Selects all book elements that are children of bookstore, where the price element's value is greater than 35.00. | | /bookstore/book[price>35.00]//title | Selects all title elements that are children of book elements within bookstore, where the price element's value is greater than 35.00. | * * * ## Selecting Unknown Nodes XPath wildcards can be used to select unknown XML elements. | Wildcard | Description | | --- | --- | | * | Matches any element node. | | @* | Matches any attribute node. | | node() | Matches any type of node. | In the table below, we list some path expressions along with their results: | Path Expression | Result | | --- | --- | | /bookstore/* | Selects all children of the bookstore element. | | //* | Selects all elements in the document. | | //title[@*] | Selects all title elements that have attributes. | * * * ## Selecting Multiple Paths By using the "|" operator in a path expression, you can select multiple paths. In the table below, we list some path expressions along with their results: | Path Expression | Result | | --- | --- | | //book/title | //book/price | Selects all title and price elements of book elements. | | //title | //price | Selects all title and price elements in the document. | | /bookstore/book/title | //price | Selects all title elements that are children of book elements within bookstore, as well as all price elements in the document.
← Xpath AxesXpath Nodes β†’