Session 16 – XPath 10/29/2018 1 Robert Kelly, 2016-2018
1
Session 16
XPath
Robert Kelly, 2016-2018
Objectives
Understand XPath well enough to provide a background to jQuery
2
Session 16 XPath 1 Objectives Understand XPath well enough to - - PDF document
Session 16 XPath Session 16 XPath 1 Objectives Understand XPath well enough to provide a background to jQuery 2 Robert Kelly, 2016-2018 10/29/2018 1 Robert Kelly, 2016-2018 Session 16 XPath Reading and References
Session 16 – XPath 10/29/2018 1 Robert Kelly, 2016-2018
1
Robert Kelly, 2016-2018
2
Session 16 – XPath 10/29/2018 2 Robert Kelly, 2016-2018
Robert Kelly, 2016-2018
Accessing XML Content
http://en.wikipedia.org/wiki/Xpath https://www.w3schools.com/xml/xpath_intro.asp
www.w3.org/TR/xpath
3 Robert Kelly, 2016-2018
4
<h3> <x:out select="$tree/Recipe/Name"/> </h3> Recipe Name Description Instruction step
… Corresponds to the tree structure of an XML document
Session 16 – XPath 10/29/2018 3 Robert Kelly, 2016-2018
Robert Kelly, 2016-2018 5
Root – unique Elements Text Attributes Comments Processing instructions namespace Note that the root node is different from the root element (the root element is a child of the root node)
Robert Kelly, 2016-2018
6
<x:out select="$tree/Recipe/Name"/>
Session 16 – XPath 10/29/2018 4 Robert Kelly, 2016-2018
Robert Kelly, 2016-2018
7
Selects the optional attribute
@optional
Robert Kelly, 2016-2018
8
/Instruction/Step
Selects the root Selects all the immediate Instruction elements (under the root) Selects all the immediate Step elements (under all the Instruction elements)
Session 16 – XPath 10/29/2018 5 Robert Kelly, 2016-2018
Robert Kelly, 2016-2018
* - matches any element node, regardless of name node() – matches element nodes as well as root node, text nodes, and attribute nodes @* - matches all attribute nodes
9
* does not match text or attribute nodes
Robert Kelly, 2016-2018 10
//Item[. = “lime gelatin”]
Selects all Item elements in the document Selects all Item elements whose value is “lime gelatin”
Session 16 – XPath 10/29/2018 6 Robert Kelly, 2016-2018
Robert Kelly, 2016-2018
If the predicate evaluates to a number, the result is true if this is the position of the context node
11
XPath indices begin at 1 (not 0)
//Item[2]
Selects the second Item element in the document
Robert Kelly, 2016-2018 12
//Item[@optional] //@units //Item[not(@*)] Selects all the Item elements with an attribute of optional Selects all units attributes Selects all Item elements without an attribute
Session 16 – XPath 10/29/2018 7 Robert Kelly, 2016-2018
Robert Kelly, 2016-2018
13 Robert Kelly, 2016-2018
14