XPath simple syntax

Source: Internet
Author: User

Use the following XML document to describe the XPath Syntax:

<? XML version = "1.0" encoding = "ISO-8859-1"?>
<Catalog>
<CD Country = "USA">
<Title> empire burlesque </title>
<Artist> Bob Dylan </artist>
<Price> 10.90 </price>
</Cd>
<CD Country = "UK">
<Title> hide your heart </title>
<Artist> Bonnie Taylor </artist>
<Price> 9.90 </price>
</Cd>
<CD Country = "USA">
<Title> Greatest Hits </title>
<Artist> Dolly Parton </artist>
<Price> 9.90 </price>
</Cd>
</CATALOG>

Locate a node:
The XML document can be viewed as a node tree (similar to a computer tree folder ).
XPath uses pattern expressions to recognize nodes in XML documents. An XPATH mode is a list of child element names separated by diagonal lines to describe a path of an XML document. Select an element that matches the path.
The following XPath expression Selects all the price elements of all CD elements of the catalog element:

/CATALOG/CD/price // if the path starts with a slash (/), it indicates the absolute path relative to an element.

Note: If the path starts with two diagonal lines, all elements that meet this condition in the document will be selected (even if they are at different XML tree levels )! The following XPath expression Selects all the price elements of all CD elements of the catalog element:
// CD // if the path starts with two diagonal lines, all elements in the document that meet this condition are selected (even if they are at different XML tree levels )! The following XPath expression Selects all the price elements of all CD elements of the catalog element:

Select unknown element
The wildcard (*) is used to select an unknown XML element.

/CATALOG/CD/* // XPath expression Selects all the child elements of all CD elements under the catalog Element
/CATALOG/*/price // XPath expression Selects all the price elements under the child element of the catalog Element
/*/Price // XPath expression Selects all the price elements with two ancestors
// * // Select all elements in the document using the XPath expression

Select Branch
You can use square brackets in an XPATH expression to specify an element.

/CATALOG/CD [1] // XPath expression selects the first CD sub-element of the catalog Element
/CATALOG/CD [last ()] // select the last child element of the catalog element in the XPath expression
/CATALOG/CD [price] // XPath expression Selects all CD elements containing the price subelement under the catalog Element
/CATALOG/CD [price = 10.90] // select all CD elements whose price value is equal to 10.90 under the catalog element in the XPath expression
/CATALOG/CD [price = 10.90]/price // The XPath expression selects the price element of all CD elements whose price value is equal to 10.90 under the catalog Element

Select multiple paths:
You can select multiple paths by using the | Operator in an XPATH expression.
The following XPath expression Selects all the title and artist elements under the child element Cd of the catalog element:

/CATALOG/CD/Title |/CATALOG/CD/artist // The XPath expression Selects all the title and artist elements under the child element Cd of the catalog Element
// Title | // artist // The XPath expression Selects all the title and artist elements in the document:

Select attributes:
In XPath, all attributes are prefixed.
The following XPath expression Selects all attributes named country:

// @ Country // select all attributes named country in the XPath expression
// CD [@ country] // select all the CD elements containing the country attribute in the XPath expression:
// CD [@ *] // select all the CD elements containing any attributes in the XPath expression
// CD [@ Country = 'uk '] // select all CD elements whose attribute country is UK.

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.