<? XML version = "1.0" encoding = "ISO-8859-1"?>
<Books>
<Book type = "hardback">
<Title> Atlas Shrugged </title>
<Author> Ayn Rand </author>
<ISBN> 0525934189 </ISBN>
</Book>
<Book type = "paperback">
<Title> a burnt-out case </title>
<Author> Graham Greene </author>
<ISBN> 0140185399 </ISBN>
</Book>
</Books>
Each node can be one of the following seven types: Root, element, attribute, text, and namespace) processing Instruction and comment)
All book elements: // books/book
A double Forward slash (//) indicates that all elements in the XML document that meet the search criteria are returned, and the position level of the elements in the document is ignored.
All ISBN elements:/books/book/ISBN
The following elements are returned:
<Books>
<Book type = "hardback">
<ISBN> 0525934189 </ISBN>
</Book>
<Book type = "paperback">
<ISBN> 0140185399 </ISBN>
</Book>
</Books>
All book elements titled Atlas Shrugged:/books/book [Title = "Atlas Shrugged"]
/Books/book [author]
Square brackets enable you to use attributes as search criteria
Books whose type attribute value is Hardback: // book [@ type = "hardback"]
Return the following elements:
<Book type = "hardback">
<Title> Atlas Shrugged </title>
<Author> Ayn Rand </author>
<ISBN> 0525934189 </ISBN>
</Book>
The square brackets symbol is called a predicate in the XPath document. Another purpose of square brackets is to identify the number of items to be retrieved.
Returns the first book element:/books/book [1].
<Book type = "hardback">
<Title> Atlas Shrugged </title>
<Author> Ayn Rand </author>
<ISBN> 0525934189 </ISBN>
</Book>
With the wildcard (*), you can easily retrieve all elements under the root node.
All book elements:/books /*
Book elements with the type value of hardcover or softcover: // books/book [@ type = "hardcover"] | // books/book [@ type = "softcover"]
The symbol (|) is the same as the logical or operator.
The first character of a statement determines the reference point. A statement starting with a forward slash (/) is considered as an absolute path during query.
Example of using relative paths:
Start searching under the current reference point
Book /*
A single dot indicates that the current node is selected, and a dual circle indicates that the parent node is selected.
All child nodes of the parent node of the current node:
../*
========================================================== ========================================================== ====================================
<A id = "A1">
<B id = "B1">
<C id = "C1">
<B name = "B"/>
<D id = "d1"/>
<E id = "E1"/>
<E id = "E2"/>
</C>
</B>
<B id = "B2"/>
<C id = "C2">
<B/>
<D id = "D2"/>
<F/>
</C>
<E/>
</A>
/A/B/C [1]Element A → Element B whose name is the first child of Element C
/A/B/C [last ()]Element A → Element B → the last child element of Element C whose ID value is E2
/A/B/C [position ()> 1]Element A → Element B → Element D with a position greater than 1 under Element C whose ID value is D1 and two Eelements with an ID value
// B [@ ID]All the two B elements with attribute IDs: B1 and B2.
// B [@ *]All B elements with the property have two B elements with the ID property and one B element with the name Property
// B [not (@ *)]All B elements not having attributes, element A → B elements under Element C
// B [@ ID = "B1"]Element B under Element A whose ID is B1
// E/parent ::*Element A whose parent node element ID is A1 and element C whose ID is C1
// F/ancestor ::*Element A whose ID is A1 and element C whose ID is C2
/A/child ::*Child element ID of a is B element of B1 and B2, C element with ID value of C2, and Eelement without any attribute
/A/descendant ::*All descendant elements of a except element
// F/self ::*All F's own element F element itself
// F/ancestor-or-self ::*All f elements and their ancestor node elements F elements, f elements parent node C elements and a Elements
/A/C/descendant-or-self ::*All Element A → element C and its child element B, Element D, and element F whose ID is C2
/A/C/following-Sibling ::*Element A → the adjacent back-order of Element C all sibling node elements do not have any attributes of the Eelement
/A/C/preceding-Sibling ::*Element A → two B elements whose IDs are B1 and B2.
/A/B/C/following ::*Element A → Element B → all elements in the descending order of Element C whose IDs are B2 B, element C without attributes, Element B without attributes, Element D with ID D2, F element without attributes and Eelement without attributes.
/A/C/preceding ::*Element A → all elements prior to element C whose ID is B2, Element B whose ID is E2, element e whose ID is e1, Element D whose ID is D1, and Element B whose name is element B, element C whose ID is C1, and Element B whose ID is B1