Basic Xpath usage

Source: Internet
Author: User

XML is often used in programming.. Net FrameWork provides DLL specially for XML processing, which provides a lot of XML Processing Methods. Here we will briefly introduce how to use XPath.

XPath uses path expressions to select nodes or node sets in XML documents. This is similar to using regular expressions to perform purposeful matching on texts.

First, write a common XML document.

<? Xml version = "1.0" encoding = "UTF-8"?>
<School>
<Class>
<Number> 1 </number>
<Teacher property = "English"> Mr Sun </teacher>
<Student_count> 50 </student_count>
</Class>
<Class>
<Number> 2 </number>
<Teacher property = "Chinese"> Mrs Li </teacher>
<Student_count> 35 </student_count>
</Class>
<Class>
<Number> 3 </number>
<Teacher property = "math"> Mr Zhang </teacher>
<Student_count> 20 </student_count>
</Class>
</School>

 

 Basic XPath usage

Expression Description
Nodename Select all child nodes of this node
/ Select from Root Node
// Select the nodes in the document from the current node that matches the selected node, regardless of their location
. Select current node
.. Select the parent node of the current node
@ Select attributes
Instance

In the following table, we have listed some path expressions and expression results:

Path expression Result
School Select all child nodes of the school element
/School

Select the root element school

Note: If the path starts with a forward slash (/), the path always represents the absolute path to an element!

School/class Select the class elements of all child elements of school.
// Class Select All book child elements regardless of their location in the document.
School // book Select all the class elements that belong to the children of the school element, regardless of their location under the school.
// @ Property Select all properties named property.

We create a console application to operate the XML using XPath:

Namespace xpathtest
{
Class Program
{
Static void main (string [] ARGs)
{

XmlDocument doc = new XmlDocument (); // create an XMLDocument object
Doc. Load (".. \ TestXML. xml"); // Load the XML file according to the path of the XML file
}


}
}

 

Now we need to get all the information in the class with number 1:

XmlNodeList list = doc. SelectNodes ("// class [number = 1]"); // obtain the class of the child node whose number is 1 in XML

Console. Write (list [0] ["number"]. Name + ":" + list [0] ["number"]. InnerText + "\ n ");

Console. Write (list [0] ["teacher"]. Name + ":" + list [0] ["teacher"]. InnerText + "\ n ");

Console. Write (list [0] ["student_count"]. Name + ":" + list [0] ["student_count"]. InnerText + "\ n ");

Console. Read ();

The result is as follows:

Number: 1

Teacher: Mr Sun

Student_count: 50

 

Some friends may say: I didn't see how simple it is. I 'd better use While (XmlReader. Read (). Don't worry. The advantages of XPath have not yet been reflected.

 

Now we have changed our requirements. We need to find the number of students in the Chinese instructor class, that is, student_count of the teacher class whose property is Chinese (a bit round ):

XmlNodeList list = doc. SelectNodes ("// class [teacher [@ property = 'English ']"); // matches the class in the teacher node where property is English

Console. Write (list [0] ["student_count"]. Name + ":" + list [0] ["student_count"]. InnerText );

Console. Read ();

 

Result:

Student_count: 35

 

We also need to find information about all the classes with a small number of students and 40:

XmlNodeList list = doc. SelectNodes ("// class [student_count <40]"); // matches student_count with a class smaller than 40

For (int I = 0; I <list. Count; I ++)

{

Console. Write ("matched" + I + "Node information ");

Console. Write (list [I] ["number"]. Name + ":" + list [I] ["number"]. InnerText + "\ n ");

Console. Write (list [I] ["teacher"]. Name + ":" + list [I] ["teacher"]. InnerText + "\ n ");

Console. Write (list [I] ["student_count"]. Name + ":" + list [I] ["student_count"]. InnerText + "\ n ");

Console. Read ();

}

 

Result:

Matching information of 1st nodes:

Number: 2

Teacher: Mrs Li

Student_count: 35

Matching information of 2nd nodes:

Number: 3

Teacher: Mr Zhang

Student_count: 35

 

Is it easy? This is the advantage of XPath:You do not need to traverse the results again and again. You only need to use one expression to get the expected results.

 

There are also some basic XPath syntaxes:

Instance

In the following table, we list some path expressions with predicates and the results of the expressions: (the predicates are matching conditions in square brackets)

Path expression Result
/School/class [1] Select the first class element that belongs to the school child element.
/School/class [last ()] Select the last class element that belongs to the school child element.
/School/class [last ()-1] Select the penultimate class element that belongs to the school child element.
/School/class [position () <3] Select the first two class elements that are child elements of the school element.
// Teacher [@ property] Select all teacher elements with properties named property.
// Teacher [@ property = 'English '] Select all teacher elements with the property value of English.
/School/class [student_count> 35.00] Select the class element of all school elements, and the student_count element must have a value greater than 35.00.
/School/class [student_count> 35.00]/title Select the teacher element of the class element of all school elements, and the value of the student_count element must be greater than 35.00.
Select unknown Node

The XPath wildcard can be used to select unknown XML elements.

Wildcard Description
* Match any element node
@* Match any attribute node
Node () Match any type of Node
Instance

In the following table, we list some path expressions and the results of these expressions:

Path expression Result
/School /* Select all child nodes of the school element
//* Select all elements in the document
// Property [@ *] Select all the property elements with attributes.
Select several paths

You can select several paths by using the "|" operator in the path expression.

Instance

In the following table, we list some path expressions and the results of these expressions:

Path expression Result
// Class/teacher | // class/number Select the teacher and number elements of all class elements.
// Teacher | // number Select All documents
/School/class/teacher | // number Select the teacher element of all the class elements of the school element and all the number elements in the document.
XPath also has multiple built-in functions for processing and comparing strings, values, and dates. here we will not explain the charm of XPath. I am a beginner and hope to work with you.

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.