XPath Study Notes

Source: Internet
Author: User

XPath is a language used to search for information in XML documents. XPath can be used to traverse elements and attributes in XML documents.

We will use this XML document in the following example.

<? 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>

Select Node
XPath uses path expressions to select nodes in XML documents. Nodes are selected by following the path or step.

The most useful path expressions are listed below:
Expression description
Nodename Selects all subnodes of this node.
/Select from the root node.
// Select the nodes in the document from the current node that matches the selected node, regardless of their location.
. Select the current node.
.. Select the parent node of the current node.
@ Select attributes.

 

Instance
In the following table, we have listed some path expressions and expression results:

Path expression result
Bookstore Selects all child nodes of the bookstore element.
/Bookstore select the root element bookstore.

Note: If the path starts with a forward slash (/), the path always represents the absolute path to an element!
 
Bookstore/book Selects all the book elements that belong to the sub-elements of bookstore.
// Book Selects all the child elements of the book, regardless of their location in the document.
Bookstore // book Selects all the book elements belonging to the descendant of the bookstore element, regardless of where they are located under the bookstore.
// @ Lang select all attributes named Lang.

 

Predicates)
It is used to find a specific node or a node that contains a specified value.

The predicates are embedded in square brackets.

 

Instance
In the following table, we list some path expressions with predicates and the results of the expressions:

Path expression result
/Bookstore/book [1] select the first book element that belongs to the bookstore sub-element.
/Bookstore/book [last ()] select the last book element that belongs to the bookstore sub-element.
/Bookstore/book [last ()-1] select the last and second book elements belonging to the bookstore sub-elements.
/Bookstore/book [position () <3] select the first two bookstore sub-elements.
// Title [@ Lang] Selects all the title elements with the attribute Lang.
// Title [@ lang = 'eng'] Selects all title elements and these elements have the lang attribute with the 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
The XPath wildcard can be used to select unknown XML elements.

Wildcard description
* Match any element node.
@ * Match any attribute node.
Node () matches any type of node.

 

Instance
In the following table, we list some path expressions and the results of these expressions:

Path expression result
/Bookstore/* select all the child elements of the bookstore element.
// * Select all elements in the document.
// Title [@ *] Selects all the title elements with attributes.

 

Select several paths
You can select several paths by using the "|" operator in the path expression.

 

Instance
In the following table, we list some path expressions and the results of these expressions:

Path expression result
// Book/Title | // book/price select all the title and price elements of the book element.
// Title | // price select all the title and price elements in the document.
/Bookstore/book/Title | // price Selects all the title elements of the book element of the bookstore element and all the price elements in the document.

 

Recommended materials: http://www.w3school.com.cn/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.