XPath is used to find information in an XML document and navigate through elements or attributes.
XPath node
7 types: Elements, attributes, text, namespaces, processing directives, annotations, and document nodes (or root nodes).
Terms:
Node:
There are 7 types of nodes in XPath: elements, attributes, text, namespaces, processing instructions, annotations, and document nodes (or root nodes). The XML document is treated as a node tree. The root of a tree is called a document node or root node.
Example 1:
<?xml version= "1.0" encoding= "iso-8859-1"?><bookstore><book> <title lang= "en" >Harry Potter</title> <author>j K. rowling</author> <year>2005</year> < Price>29.99</price></book></bookstore>
Node Example:
<bookstore> document Node <author>j K. rowing</author> element node lang= "en" attribute node
Base value (or atomic value, Atomic value)
The base value is a node that has no parent or no child.
Basic value Examples:
J K. Rowing "en"
Project (item)
A project is a base value or node.
Node relationships
Father (parent)
Each element and attribute has a parent
Example 2:
<book> <title>harry potter</title> <author>j K. rowling</author> < Year>2005</year> <price>29.99</price></book>
In this example, book is the parent of the title, author, year, and price elements.
Sub (children)
An element node may have 0, one, or more child.
In this example, Titile, author, year, and Price are the children of the book element.
Fellow (sibling)
In this example, the title, author, year, and price elements are all compatriots.
Ancestors (ancestor)
In Example 1, the ancestor of the title element is the book element and the bookstore element.
Descendants (descendant)
In Example 1, the descendants of the bookstore element are the book, title, author, year, and price elements.
XPath uses a path expression to pick a node or set of nodes in an XML document. A node is picked up either along a path or a step (steps).
XML Instance Document
<?xml version= "1.0" encoding= "iso-8859-1"?><bookstore><book> <title lang= "Eng" >Harry potter</title> <price>29.99</price></book><book> <title lang= "eng" > Learning xml</title> <price>39.95</price></book></bookstore>
XPath uses a path expression to select a node in the XML document. Nodes are selected by path or step.
Common path expressions
An expression |
Description |
NodeName |
Select all child nodes of this node. |
/ |
Select from the root node. |
// |
Selects the nodes in the document from the current node that matches the selection, regardless of their location. |
. |
Select the current node. |
.. |
Selects the parent node of the current node. |
@ |
Select the attribute. |
Instance
Path Expression |
Results |
Bookstore |
Selects all child nodes of the bookstore element. |
/bookstore |
Select the root element bookstore. Note: If the path starts with a forward slash (/), this path always represents the absolute path to an element! |
Bookstore/book |
Selects all book elements that belong to a child element of bookstore. |
Book |
Selects all book child elements, regardless of their position in the document. |
Bookstore//book |
Selects all book elements that belong to descendants of the bookstore element, regardless of where they are located under bookstore. |
@lang |
Select all attributes that are named Lang. |
predicate (predicates)
To find a particular node or a node that contains a specified value.
The predicate is embedded in square brackets.
Instance:
Path Expression |
Results |
/BOOKSTORE/BOOK[1] |
Selects the first book element that belongs to a bookstore child element. |
/bookstore/book[last ()] |
Select the last book element that belongs to the bookstore child element. |
/bookstore/book[last ()-1] |
Select the second-to-last book element that belongs to the bookstore child element. |
/bookstore/book[position () <3] |
Select the first two book element that belongs to the child element of the bookstore element. |
title[@lang] |
Select all the title elements that have properties named Lang. |
title[@lang = ' Eng '] |
Selects all title elements, and these elements have the lang attribute value of Eng. |
/BOOKSTORE/BOOK[PRICE>35.00] |
Selects all the book elements of the bookstore element, and the value of the price element must be greater than 35.00. |
/bookstore/book[price>35.00]/title |
Selects all the title elements of the book element in the bookstore element, and the value of the price element must be greater than 35.00. |
Select Unknown node
XPath wildcard characters can be used to select unknown XML elements.
wildcard characters |
Description |
* |
Matches any element node. |
@* |
matches any attribute node. |
Node () |
Matches any type of node. |
Instance
Path Expression |
Results |
/bookstore/* |
Selects all child elements of the bookstore element. |
//* |
Selects all elements in the document. |
Title[@*] |
Select all the title elements with attributes. |
Select several paths
By using the ' | ' in a path expression operator, you can select a number of paths.
Instance
Path Expression |
Results |
Book/title | Book/price |
Selects all the title and price elements of the book element. |
Title | Price |
Selects all the title and price elements in the document. |
/bookstore/book/title | Price |
Selects all the title elements of the book element that belong to the bookstore element, and all the price elements in the document. |
Http://www.w3school.com.cn/xpath/index.asp
XPath Learning notes