XPath
• XPath (XML Path Language) is a common query method used to query and locate elements and texts in XML documents.
• Many people regard XPath as an Internet SQL language.
• The XPath syntax uses a pattern called an expression. There is nothing in the result set during initialization. The XPath expression is used to make the nodes that appear in the result set form a specific hierarchy and meet certain conditions.
• XPath context: it is a specific node in the document tree where we will query the document. It can be considered as a starting point of the query.
XPath expressions
• Consists of location steps. It consists of an axis, a node test, and a series of optional predicates. You can use a backslash to connect multiple locations to form a location path and generate a group of nodes as the result.
•/Books/book/title: all the sub-element titles of the sub-element book of the element Books.
•/Books/book [@ Price <21.99]/title: returns all the title sub-elements of the book whose Price attribute value is less than 21.99 in the book sub-element under the root element of Books.
• Position step: each part separated by a backslash in the expression is called a position step.
• Axis: A part of the document relative to the context node. It defines a group of nodes with specific levels of relationships with the current node.
• Node test: Any expression used to indicate a combination of nodes in a location path. In the node test, the initial result set is filtered by name or type.
-Child: text (): returns all subfile nodes.
• Predicate: an expression of true or false.
Axis
• Axes include self, child, parent, descendent, ancestor, attribute, namespace, following, and preceding.
• Child: Customer: returns all Customer elements in the child element of the current node.
• Descendent: OrderItem: return all descendants of the node named OrderItem
XPath expression example
•./Order will find all elements named Order in the current context
•/Order will find all elements named Order under the root of the Document Tree
• // Order will find all the elements named Order anywhere in the Document Tree, regardless of depth or hierarchy
• Child: book [attribute: publisher = 'zhang san']
• Child: book [@ publisher = 'zhang san']
• Descendent: book [count (child: chapter)> 5]: Use
The count function retrieves all child book nodes with more than 5 chapter ter
• Child: book [start-with (attribute: publisher, 'zhang ')]:
Use the start-with function to retrieve all publisher attributes starting with 'zhang'
Book subnode.
XPath abbreviations
• // Represents the descendant axis
• @ Represents the attribute axis
• Represent Yourself
•... Represents the parent node
XML
<? Xml version = "1.0" encoding = "UTF-8"?>
<Orders>
<Customer Name = 'Albert '>
<Order Quantity = "12">
Roast Duck
</Order>
<Order Quantity = "5">
Red Wine
</Order>
</Customer>
<Customer Name = 'john'>
<Order Quantity = "3">
French Fries
</Order>
<Order Quantity = "4">
Coffee
</Order>
</Customer>
<Customer Name = 'Stephen '>
<Order Quantity = "5">
Milk
</Order>
</Customer>
<Customer Name = 'Alice '>
<Order Quantity = "18">
Frozen Pizza
</Order>
<Order Quantity = "3">
Potato Chips
</Order>
</Customer>
</Orders>
Program:
Protected void Page_Load (object sender, EventArgs e)
{
XmlDocument xmlDocument = new XmlDocument ();
// Load Xml
XmlDocument. Load (Server. MapPath ("Test. xml "));
// Query nodes whose names start with
XmlNodeList xmlNodeList = xmlDocument. DocumentElement. SelectNodes ("// Customer [starts-with (@ Name, 'A')]/Order ");
// Find several nodes in total
Response. Write ("Found:" + xmlNodeList. Count. ToString () + "node <br> ");
Foreach (XmlNode xmlNode in xmlNodeList)
{
// Obtain the Value of the Name Node
String parentNodeAttribute = xmlNode. ParentNode. Attributes. GetNamedItem ("Name"). Value;
// Obtain the Value of Quantity
String elementAttribute = xmlNode. Attributes. GetNamedItem ("Quantity"). Value;
// Display
Response. Write ("Customer Name:" + parentNodeAttribute + "Ordered Quantity:" + elementAttribute + "" + xmlNode. InnerText + "<br> ");
}
}