Powerful XPath (1)

Source: Internet
Author: User

Record the powerful XPATH in my memory. Its previous glory caught me. But now I feel like I'm getting OUT of it. Maybe it's my own feeling. I'm using less!

XPath is a language used to search for information in XML documents. XML is its most intimate Friend. XPath can be used to traverse elements and attributes in XML documents. XQuery and XPointer are built on it at the same time, so it has many advantages in many places. Let's talk about its main language components.

1: XPath path

Its path expression is very similar to the one we see in the computer file system. It is even more accurate than the file system. XPath uses a path expression to select a node or a set of multiple nodes in the XML document. Nodes are selected by traversing the element path in the XML document.

 

The most common path expressions are as follows (the button for inserting a table is not found, and the line breaks are not displayed in the groubox, which is hard to see)

 

Code Node name (nodename): Select All subnodes under nodename

/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

First, the following is a test XML document with the structure: School> class {grade, class teacher}

Code <? Xml version = "1.0" encoding = "uft8"?>
<School>
<Class>
<Grade number = "0302"> second class </grade>
<Master> instructor Li </master>
</Class>
<Class>
<Grade number = "0601"> six-year class 1 </grade>
<Master> instructor Wang </master>
</Class>
</School>

 

Test path expression:

Code Test path expression:

School: select all child nodes under the school element (all classes under the school)

/School:/indicates the absolute path, indicating to select the root node school (select a single school)

School/class: select the child elements of all classes in school (select all child nodes in all classes in school)

// Class: Selects all class child elements regardless of their position in the document.

School // class: select all class nodes under school, regardless of their location under school

// @ Number: select all attributes named number (select all class numbers)

School/class [number = '000000']/master: Find the class teacher of the class numbered "0302"

It can be seen that XPath is very powerful. With expressions, we do not need to traverse the XML document structure to find the values you need.

To be continued .....

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.