Introduction to XPath

 

Introduction to XPath

 

What is XPath

 

XPath stands for XML Path Language, a language used to locate parts of an XML (a subset of the standard Universal Markup Language) document. XPath is based on the tree structure of XML , which provides the ability to find nodes in the data structure tree. For detailed grammar tutorials, please refer to W3School 's XPath .

 

Principles of XPath

 

XPath converts XML data into a DOM tree structure and provides the ability to find nodes in the data structure tree.

 

XPath syntax

 

XPath uses path expressions to select nodes in an XML document (string), and nodes are selected by following a path. The path consists of node names, separated by "/" . The following is an example to illustrate:

 

Test XML data

 

<?xml version="1.0" encoding="ISO-8859-1"?>

 

<bookstore>

 

  <book category="COOKING">

 

    <title lang="en">Everyday Italian</title>

 

    <author>Giada De Laurentiis</author>

 

    <year>2005</year>

 

    <price>30.00</price>

 

  </book>

 

  <book category="CHILDREN">

 

    <title lang="en">Harry Potter</title>

 

    <author>JK.Rowling</author>

 

    <year>2005</year>

 

    <price>29.99</price>

 

  </book>

 

  <book category="WEB">

 

    <title lang="en">Learning XML</title>

 

    <author>ErikT.Ray</author>

 

    <year>2003</year>

 

    <price>39.95</price>

 

  </book>

 

</bookstore>

 

1.     Select the price node value of all books

 

2.     xpath: /bookstore/book/price/text()

 

3.     Return : [30.00,29.99,39.95]

 

4.     The conditional expression selects the title value of the book whose author is J K. Rowling

 

5.     xpath: /bookstore/book[author="JK.Rowling"]/title/text()

 

6.     Back : Harry Potter

 

7.     Attribute value processing Select the lang attribute value of the title of the book whose category is "WEB"

 

8.     xpath: /bookstore/book[@category="WEB"]/title/@lang

 

9.     Return : en

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326306458&siteId=291194637