C # Use of xpath [reprint]

Source: Internet
Author: User

XPath uses path expressions to select nodes or node sets in XML documents. Nodes are selected by following the path or step (steps. XML instance document

We will use this XML document in the following example.

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

<Bookstore>

<Book>
<Title lang = "eng"> Harry Potter </title>
<Price> 29.99 </price>
</Book>

<Book>
<Title lang = "eng"> learning XML </title>
<Price> 39.95 </price>
</Book>

</Bookstore>

Select Node

XPath uses path expressions to select nodes in XML documents. Nodes are selected by following the path or step.
The most useful path expressions are listed below:
Expression description
Nodename Selects all the subnodes of this node.
/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.
Instance

In the following table, we have listed some path expressions and expression results:
Path expression result
Bookstore Selects all the subnodes of the bookstore element.
/Bookstore

Select the root element bookstore.

Note: If the path starts with a forward slash (/), the path always represents the absolute path to an element!
Bookstore/book Selects all the book elements that belong to the sub-elements of bookstore.
// Book Selects all the child elements of the book, regardless of their location in the document.
Bookstore // book Selects all the book elements belonging to the descendant of the bookstore element, regardless of where they are located under the bookstore.
// @ Lang select all attributes named Lang.
Predicates)

It is used to find a specific node or a node that contains a specified value.

The predicates are embedded in square brackets.
Instance

In the following table, we list some path expressions with predicates and the results of the expressions:
Path expression result
/Bookstore/book [1] select the first book element that belongs to the bookstore sub-element.
/Bookstore/book [last ()] select the last book element that belongs to the bookstore sub-element.
/Bookstore/book [last ()-1] select the last and second book elements belonging to the bookstore sub-elements.
/Bookstore/book [position () <3] select the first two bookstore sub-elements.
// Title [@ Lang] Selects all the title elements with the attribute Lang.
// Title [@ lang = 'eng'] Selects all title elements and these elements have the lang attribute with the value of ENG.
/Bookstore/book [price> 35.00] Selects all the book elements of the bookstore element, and the value of the price element must be greater than 35.00.
/Bookstore/book [price> 35.00]/Title Selects all the title elements of the book element in the bookstore element, and the value of the price 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 () matches any type of node.
Instance

In the following table, we list some path expressions and the results of these expressions:
Path expression result
/Bookstore/* select all the child elements of the bookstore element.
// * Select all elements in the document.
// Title [@ *] Selects all the title 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
// Book/Title | // book/price select all the title and price elements of the book element.
// Title | // price select all the title and price elements in the document.
/Bookstore/book/Title | // price Selects all the title elements of the book element of the bookstore element and all the price elements in the document.
XPath axis

The axis defines the node set relative to the current node.
Axis name result
Ancestor Selects all the ancestors of the current node (parent, grandfather, etc ).
Ancestor-or-self Selects all the ancestors of the current node (parent, grandfather, etc.) and the current node itself.
Attribute Selects all attributes of the current node.
Child Selects all child elements of the current node.
Descendant Selects all descendant elements (child, sun, etc.) of the current node ).
Descendant-or-self Selects all child elements (child, sun, etc.) of the current node and the current node itself.
Following Selects all nodes after the end label of the current node in the document.
Namespace: Select All namespace nodes of the current node.
Parent selects the parent node of the current node.
Preceding Selects all nodes before the start label of the current node in the document.
Preceding-sibling Selects all peer nodes before the current node.
Self selects the current node.
Location Path expression

The location path can be absolute or relative.

The absolute path starts with a forward slash (/), but the relative path does not. In either case, the path contains one or more steps, and each step is separated by a slash:
Absolute path:

/Step /...

Relative Path:

STEP/step /...

Each step is calculated based on the nodes in the current node set.
Instance
Example result
Child: Book Selects all the book nodes that belong to the child elements of the current node.
Attribute: Lang selects the lang attribute of the current node.
Child: * select all child elements of the current node.
Attribute: * select all attributes of the current node.
Child: Text () select all text subnodes of the current node.
Child: node () selects all child nodes of the current node.
Descendant: Book Selects all book descendants of the current node.
Ancestor: Book Selects all the books of the current node.
Ancestor-or-self: Book Selects all the book predecessors of the current node and the current node (if this node is a book node)
Child: */child: Price: select all price sun nodes of the current node.

XPath expressions can return node sets, strings, logical values, and numbers.
XPath Operators

Operators available in XPath expressions are listed below:
Operator description instance Return Value
| Calculate two node sets // book | // CD returns all node sets with book and CD Elements
+ Addition 6 + 4 10
-Subtraction 6-4 2
* Multiplication 6*4 24
Div Division 8 Div 4 2
= Equal to price = 9.80

If the price is 9.80, true is returned.

If the price is 9.90, false is returned.
! = Not equal to price! = 9.80

If the price is 9.90, true is returned.

If the price is 9.80, false is returned.
<Less than price <9.80

If the price is 9.00, true is returned.

If the price is 9.90, false is returned.
<= Less than or equal to price <= 9.80

If the price is 9.00, true is returned.

If the price is 9.90, false is returned.
> Greater than price> 9.80

If the price is 9.90, true is returned.

If the price is 9.80, false is returned.
>=Greater than or equal to price> = 9.80

If the price is 9.90, true is returned.

If the price is 9.70, false is returned.
Or price = 9.80 or price = 9.70

If the price is 9.80, true is returned.

If the price is 9.50, false is returned.
And and price> 9.00 and price <9.90

If the price is 9.80, true is returned.

If the price is 8.50, false is returned.
MoD calculates the remainder of division 5 mod 2 1

Load XML document

All modern browsers support using XMLHttpRequest to load XML documents.

Code for most modern browsers:

VaR XMLHTTP = new XMLHttpRequest ()

Code for the old Microsoft Explorer (IE 5 and 6:

VaR XMLHTTP = new activexobject ("Microsoft. XMLHTTP ")

Select Node

Unfortunately, Internet Explorer is different from other methods for processing XPath.

In our example, we include code applicable to most mainstream browsers.

Internet Explorer uses the selectnodes () method to select nodes from the XML document:

Xmldoc. selectnodes (XPath );

Firefox, chrome, opera, and Safari use the evaluate () method to select nodes from the XML document:

Xmldoc. Evaluate (XPath, xmldoc, null, xpathresult. any_type, null );

Select all titles

The following example Selects all title nodes:

/Bookstore/book/Title

Select the title of the first book

The following example selects the title of the first book node under the bookstore element:

/Bookstore/book [1]/Title

Here is a problem. The preceding example outputs different results in IE and other browsers.

Ie5 and later versions regard [0] as the first node, and according to W3C standards, it should be [1].

To solve the [0] and [1] Problems in ie5 +, you can set the language selection (selectionlanguage) for xpath ).

The following example selects the title of the first book node under the bookstore element:

XML. setproperty ("selectionlanguage", "XPath ");
XML. selectnodes ("/bookstore/book [1]/Title ");

Select All prices

The following example Selects all texts in the price node:

/Bookstore/book/price/text ()

Select Price nodes with prices higher than 35

The following example Selects all price nodes with prices higher than 35:

/Bookstore/book [price> 35]/price

Select a title node with a price higher than 35

The following example Selects all title nodes with prices higher than 35:

/Bookstore/book [price> 35]/Title

Try it yourself
Select All book nodes

The following example Selects all the book nodes under the bookstore element:

Xmldoc. selectnodes ("/bookstore/book ")

Select the first book Node

In the following example, only the first book node under the bookstore element is selected:

Xmldoc. selectnodes ("/bookstore/book [0]")

Note: in IE 5 and 6, [0] will be executed as the first node. However, according to W3C standards, [1] should be used.!

Note: this problem has been corrected in IE 6 SP2!
Select Price

The following example selects text from all price nodes:

Xmldoc. selectnodes ("/bookstore/book/price/text ()")

Select a price above 35

The following example Selects all price nodes with prices higher than 35:

Xmldoc. selectnodes ("/bookstore/book [price> 35]/price ")

Select a title node with a price higher than 35

The following example Selects all title nodes with prices higher than 35:

Xmldoc. selectnodes ("/bookstore/book [price> 35]/Title ")

Related Article

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.