Summary of common XPath locating node element statements

Source: Internet
Author: User
Tags xpath examples xpath functions xpath contains

After converting an XML or HTML document into a DOM tree structure, how can we locate a specific node? XPath enables this function to navigate through the path and attributes of nodes in the DOM tree. Through the XPath path expression, you can select nodes (nodes) in the DOM tree) or node-set ).

XPath contains more than 100 built-in functions. These functions are used to compare string values, numeric values, date and time, segment operations, sequential operations, Boolean values, and so on.

Java package for parsing

Java 5 introduces the javax. xml. XPath package, which is a library used for xpath document query independent of the XML object model.


Common functions

Common functions of xpath mainly include node set functions, string functions, Boolean functions, and numeric functions. There are many documents on the Internet, so I will not describe them here. refer to the following documents:

[A] for xpath, XQuery, and XSLT Functions

[B] XPath functions

[C] XPath functions (msdn)

Common locating statement instances

1. // node [not (@ Class)] All nodes whose names are node and do not contain the class attribute

2. // node [@ class and @ ID] All nodes named node that contain both the class and ID attributes

3. // node [contains (text (), substring] nodes whose names are node and whose text contains substring

// A [contains (text (), \ "Next Page \")] All hyperlink nodes that contain the "next page" String

// A [contains (@ title ,"ArticleTitle ")] All hyperlink nodes whose title attributes contain the" article title "String

4. // node [@ ID = "myid"]/text () all direct text subnodes of nodes whose node name is node and whose property ID is myid

5. Book [author/degree] All book nodes that contain the author node and contain at least one degree child node

6. Author [. = "Matthew Bob"] All author nodes with the value of "Matthew Bob"

7. // * [count (BBB) = 2] All nodes that contain two BBB Child Nodes

8. // * [count (*) = 2] All nodes with two child nodes

9. // * [name () = 'bbb '] All nodes named BBB, equivalent to // bbb

10. // * [starts-with (name (), 'B')] All nodes whose names start with B

11. // * [contains (name (), 'C')] All nodes whose names contain letters c

12. // * [String-length (name () = 3] nodes with names of 3 letters

13. // CCC | // All CCC or BBB nodes

14./child: AAA is equivalent to/AAA

15. // CCC/descendant: * all nodes whose CCC is their ancestor

16. // DDD/parent: * All parent nodes of the DDD Node

17. // BBB [position () mod 2 = 0] BBB node at an even position

18. Author [not (last-name = "Bob")] All nodes that do not contain the value of the last-name element Bob

19. P/text () [2] The second text node of the P node in the current context node

20. Ancestor: Book [1] Book ancestor node closest to the current context node

21. // A [text () = "Next"] the content of the anchor text is equal to that of node A of next.



Finally, we recommend an XPATH plug-in Firefox:

XPath checker


This plug-in allows you to easily view the path of any element on the webpage, but the automatically generated path to the XPath is usually not the simplest path.



[1] XPath examples.

[2] XPath tutorial Http://

[3] How does XPath introduce Boardid = 14 & id = 35493

[4] The XPath reference

[5] XML Path Language (XPath) Version 1.0

[6] XPath tutorial

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: 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.