Getting started with XPath syntax rules

Source: Internet
Author: User

 

XPath syntax rules

XPath uses path expressions to determine nodes in XML documents.

We will use the following XML document to describe the XPath syntax.

<? XML version = "1.0" encoding = "gb2312"?>



C2688-67037
LCD display
1
358.00


C2688-67061
speaker
1
16.50


C2688-67010
move the cursor
1
8.50

Node positioning:

XML text block can be expressed as a tree node
XPath uses pattern expressions to identify nodes in XML documents.

In an XPATH mode, the Backslash "/" is used to separate the sub-element names to describe the path.

The following XPath expression Selects all the price elements in the element item under the element order.
/Order/item/price

Note: Start with "/" to represent the absolute path of the element.

The relative path of the element does not start "/".

Item/price

Start with the "//" path to represent all elements that meet the conditions of the entire document

The following XPath expression Selects all the item elements in the document.
// Item

Select unknown element

The wildcard "*" can be used to select unknown XML elements.

The following XPath expression selects the child elements of all item elements in the element order.

/Order/item /*

The following XPath expression selects the price element of all grandchildren under the element order.
/Order/*/price

The following XPath expression Selects all the price elements with two ancestors
/*/Price

The following XPath expression Selects all elements of the document.
//*

Select Branch

Use square brackets [] to specify specific elements

The following XPath expression selects the child element of the first item in the element order.

/Order/item [1]

The following XPath expression selects the child element of the last item in the element order.
/Order/item [last ()]

The following XPath expression selects the item element with the price element in the element order.
/Order/item [price]

The following XPath expression selects the item element with the price equal to 12.60 element from the element order
/Order/item [price = 16.50]

The following XPath expression selects a price element with a price equal to 12.60 element from the item element that belongs to the order element.
/Order/item [price = 16.50]/price

Select several paths

In an XPATH expression, you can select several paths using the "|" operator. It is essentially a logical "and" Operation

The following XPath expression Selects all itemnumber and description elements from the item element that belongs to order
/Order/item/itemnumber |/order/item/Description

The following XPath expression Selects all itemnumber and description elements from the document.
// Itemnumber | // description

The following XPath expression Selects all itemnumber, description, and price elements from the document.
// Itemnumber | // description | // price

The following XPath expression Selects all itemnumber elements under the item in order and all description elements from the document.
/Order/item/itemnumber | // description

Select attributes
In XPath, all attributes use the @ prefix.

The following XPath expression Selects all the attributes named catalog.
// @ Catalog

The following XPath expression Selects all the item elements with the catalog attribute
// Item [@ catalog]

The following XPath expression Selects all the item elements with any attributes
// Item [@ *]

The following XPath expression Selects all the item elements with the catalog equal to "parts" attribute
// Item [@ catalog = "parts"]

 

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.