XPath Presentation
XPath Presentation
XPath Presentation
Intro
XPath is used to navigate through elements and attributes in an XML document. XPath is a major element in W3C's XSLT standard - and XQuery and XPointer are both built on XPath expressions.
Intro
XPath
is a syntax for defining parts of an XML document XPath uses path expressions to navigate in XML documents XPath contains a library of standard functions XPath is a major element in XSLT XPath is a W3C recommendation
XPath Terminology
Seven
kinds of nodes:
element attribute
Relationship of Nodes
Parent Children Siblings
Ancestors
Descendants
Selecting Nodes
Expression nodename / Description Selects all child nodes of the named node Selects from the root node
//
. .. @
Selects nodes in the document from the current node that match the selection no matter where they are
Selects the current node Selects the parent of the current node Selects attributes
<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>
Result Selects all the child nodes of the bookstore element Selects the root element bookstore Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! Selects all book elements that are children of bookstore Selects all book elements no matter where they are in the document Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element Selects all attributes that are named lang
//@lang
Predicates
Path Expression /bookstore/book[1] /bookstore/book[last()] /bookstore/book[last()-1] /bookstore/book[position()<3] Result Selects the first book element that is the child of the bookstore element. Selects the last book element that is the child of the bookstore element Selects the last but one book element that is the child of the bookstore element Selects the first two book elements that are children of the bookstore element
//title[@lang]
//title[@lang='eng'] /bookstore/book[price>35.00]
Selects all the title elements that have an attribute named lang
Selects all the title elements that have an attribute named lang with a value of 'eng' Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00
/bookstore/book[price>35.00]/title
Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00
XPath Operators
Operator | Description Computes two node-sets Addition Subtraction Multiplication Division Equal Example //book | //cd Return value Returns a nodeset with all book and cd elements 10 2 24 2 true if price is 9.80 false if price is 9.90
+ * div =
Not equal Less than Less than or equal to Greater than Greater than or equal to or and Modulus (division remainder)
price!=9.80 price<9.80 price<=9.80 price>9.80 price>=9.80 price=9.80 or price=9.70 price>9.00 and price<9.90 5 mod 2
true if price is 9.90 false if price is 9.80 true if price is 9.00 false if price is 9.80 true if price is 9.00 false if price is 9.90 true if price is 9.90 false if price is 9.80 true if price is 9.90 false if price is 9.70 true if price is 9.80 false if price is 9.50 true if price is 9.80 false if price is 8.50 1
Loading
var
Nodes
xmlDoc.evaluate(xpath,