An XPath path expression

Source: Internet
Author: User
Tags xpath

Http://www.ruanyifeng.com/blog/2009/07/xpath_path_expressions.html

Simply put, XPath is the way to select nodes in an XML file.

The so-called node, is the smallest constituent unit of the XML file, divided into 7 kinds altogether.

-Element (Elements node)
-Attributes (Attribute node)
-Text (literal node)
-Namespace (Namespace node)
-processing-instruction (Processing command node)
-Comment (annotation node)
-Root (root node) XPath can be used to select these 7 nodes. However, the following notes refer only to the first element (elements node) that is most commonly used, so you can treat the nodes and elements below as synonyms.

Basic format for XPath expressions

XPath selects nodes through the path Expression. In form, "path expressions" are very similar to traditional file systems.

# the slash (/) is used as a delimiter within the path.

# The same node has the absolute path and the relative path two kinds of writing.

# Absolute path (absolute path) must be "/" first, followed by the root node, such as/step/step/...

# The relative path (relative path) is a method of writing other than an absolute path, such as Step/step, which is not using the "/" first.

# "." Represents the current node.

# ".." Represents the parent node of the current node two, the basic rules of the selection node

-NodeName (node name): Indicates that all child nodes of the node are selected

-"/": means to select the root node

-"//": Indicates that a node is selected anywhere

-"@": indicates that a property is selected

Third, select the instance of the node

Look at an XML instance document first.

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

[Example 1]

Bookstore: Selects all child nodes of the bookstore element.

[Example 2]

/bookstore: Select the root node bookstore, which is the absolute path notation.

[Example 3]

Bookstore/book: Selects all the book elements that belong to the bookstore, which is the relative path notation.

[Example 4]

Book: Selects all book child elements, regardless of their position in the document.

[Example 5]

Bookstore//book: Select all the book elements that belong to the descendants of the bookstore element, regardless of where they are located under bookstore.

[Example 6]

@lang: Select All properties named Lang.

Iv. the predicate condition of XPath (predicate)

The so-called "predicate condition" is an additional condition for the path expression.

All of the conditions, written in brackets "[]", indicate further filtering of the nodes.

[Example 7]

/BOOKSTORE/BOOK[1]: Represents the first book child element of the selection bookstore.

[Example 8]

/bookstore/book[last ()]: Represents the last book child element of the selection bookstore.

[Example 9]

/bookstore/book[last ()-1]: Represents the penultimate book child element of the selected bookstore.

[Example 10]

/bookstore/book[position () <3]: Represents the first two book child elements that select the bookstore.

[Example 11]

title[@lang]: Indicates that all title nodes with the lang attribute are selected.

[Example 12]

title[@lang = ' Eng ']: Represents the title node that selects all Lang attributes with a value equal to "eng".

[Example 13]

/bookstore/book[price]: Represents the book child element that selects the bookstore, and the selected book element must have a price child element.

[Example 14]

/BOOKSTORE/BOOK[PRICE>35.00]: Indicates the book child element of the selected bookstore, and the price child element value of the selected book element must be greater than 35.

[Example 15]

/bookstore/book[price>35.00]/title: Indicates that the title child element is selected in the result set of example 14.

[Example 16]

/BOOKSTORE/BOOK/PRICE[.>35.00]: Indicates the price child element of "/bookstore/book" with a value greater than 35 selected.

Five, wildcard characters

# "*" means to match any element node.

# "@*" indicates that any property value is matched.

# node () represents a node that matches any type. [Example 17]

*: Selects all ELEMENT nodes in the document.

[Example 18]

/*/*: Represents the selection of all the second-tier element nodes.

[Example 19]

/bookstore/*: Represents the selection of all element child nodes of the bookstore.

[Example 20]

Title[@*]: Represents the selection of all title elements with attributes.

Six, select multiple paths

with "|" Select multiple parallel paths.

[Example 21]

//book/title |//book/price: Represents the title child element and the price child element that selects the book element at the same time.

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.