1 xpath 1.0 roger l. costello 6 march 2010
TRANSCRIPT
![Page 1: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/1.jpg)
1
XPath 1.0
http://www.w3.org/TR/xquery/
Roger L. Costello6 March 2010
![Page 2: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/2.jpg)
2
XPath is not a standalone language
XPath
XSLT
XQuery
XMLSchemas
XPointer
Schematron
A complete list of technologies that use XPath:http://expath.org/wiki/Engines
![Page 3: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/3.jpg)
3
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
<?xml version="1.0"?><FitnessCenter> <Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor> </Member> <Member> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor> </Member> <Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor> </Member></FitnessCenter>
This XML document can berepresented as a tree, as shown below
![Page 4: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/4.jpg)
4
Terminology: node
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
Document nodeProcessing Instruction (PI) node
Element nodes
Text nodes
![Page 5: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/5.jpg)
5
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
With respect to this node, these are its children
![Page 6: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/6.jpg)
6
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
These are its descendant nodes
![Page 7: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/7.jpg)
7
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
This is the context node
![Page 8: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/8.jpg)
8
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
That's its parent
![Page 9: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/9.jpg)
9
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
Those are its ancestors
![Page 10: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/10.jpg)
10
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
It has 2 siblings
![Page 11: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/11.jpg)
11
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
They are following-siblings
![Page 12: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/12.jpg)
12
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
It has no preceding-siblings
![Page 13: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/13.jpg)
13
Here are the capabilities of XPath
• XPath provides a syntax for: – navigating around an XML document– selecting nodes and values – comparing node values– performing arithmetic on node values
• XPath provides some functions (e.g., concat(), substring()) to facilitate the above.
![Page 14: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/14.jpg)
14
Document/
PI<?xml version=“1.0”?>
ElementDocument
ElementPara
ElementPara
ElementPara
TextOne if …
TextAnd I …
TextReady to
…
<?xml version="1.0"?><Document classification="secret"> <Para classification="unclassified"> One if by land, two if by sea; </Para> <Para classification="confidential"> And I on the opposite shore will be, Ready to ride and spread the alarm </Para> <Para classification="unclassified"> Ready to ride and spread the alarm Through every Middlesex, village and farm, </Para></Document>
This XML document can berepresented as a tree, as shown below
Attributeclassification=“secret”
Attributeclassification=“unclassified”
Attributeclassification=“confidential”
Attributeclassification=“unclassified”
![Page 15: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/15.jpg)
15
Execute XPath using Oxygen XML
Type your XPath expression here
Change this to XPath 1.0
![Page 16: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/16.jpg)
16
Use XPath Builderfor long XPathexpressions
![Page 17: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/17.jpg)
17
Please Execute the XPath Expressions
• The following slides contain XPath expressions.
• It is important that you copy the expression on the slide and paste it into Oxygen XML to see what the expression does.
• Now, drag and drop Document.xml (in the example000 folder) into Oxygen XML.
![Page 18: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/18.jpg)
18
Select all Para Elements
/Document/Para
![Page 19: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/19.jpg)
19
Document/
PI<?xml version=“1.0”?>
ElementDocument
ElementPara
ElementPara
ElementPara
TextOne if …
TextAnd I …
TextReady to
…
Attributeclassification=“secret”
Attributeclassification=“unclassified”
Attributeclassification=“confidential”
Attributeclassification=“unclassified”
/Document/Para
“Hey XPath processor, position yourself at the Document node (i.e., /); use that as the ‘context node’; from there select any child elements with the name ‘Document’ (there’s only one); now use that as the context node and from there select any child elements with the name Para (there are four, although only three are shown below due to space limitations). Show the selected set of nodes.”
![Page 20: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/20.jpg)
20
Result
<Para classification="unclassified"> One if by land, two if by sea;</Para><Para classification="confidential"> And I on the opposite shore will be, Ready to ride and spread the alarm</Para><Para classification="unclassified"> Ready to ride and spread the alarm Through every Middlesex, village and farm,</Para><Para classification="secret"> For the country folk to be up and to arm.</Para>
Evaluating the XPath query results in selecting each Para element, including its attribute and contents.
![Page 21: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/21.jpg)
21
Here’s how Oxygen displays the results
Please understand what this is saying: the first Para element (Para[1]) and its content is selected, the second Para element and its content is selected, the third Para and its content is selected, and the fourth Para element and its content is selected.
![Page 22: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/22.jpg)
22
/Document/Para
This is an absolute XPath expression. i.e., the selection of nodes starts from the top of the document (tree).
Absolute XPath Expression
![Page 23: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/23.jpg)
23
Not XML!
• XPath syntax is not XML syntax!
![Page 24: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/24.jpg)
24
Establish a Context Node
Click on this to establish Document as the "context node"(this will allow you to create XPath expressions that are relative to it)
![Page 25: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/25.jpg)
25
Relative XPath Expression
In Oxygen XML click on <Document> toestablish the context node and then typethis in the XPath box:
Para
![Page 26: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/26.jpg)
26
Result
<Para classification="unclassified"> One if by land, two if by sea;</Para><Para classification="confidential"> And I on the opposite shore will be, Ready to ride and spread the alarm</Para><Para classification="unclassified"> Ready to ride and spread the alarm Through every Middlesex, village and farm,</Para><Para classification="secret"> For the country folk to be up and to arm.</Para>
Same result as before.
![Page 27: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/27.jpg)
27
Select “here”
In Oxygen XML click on <Document> toestablish the context node and then typethis in the XPath box:
.
![Page 28: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/28.jpg)
28
Result<Document classification="secret"> <Para classification="unclassified"> One if by land, two if by sea; </Para> <Para classification="confidential"> And I on the opposite shore will be, Ready to ride and spread the alarm </Para> <Para classification="unclassified"> Ready to ride and spread the alarm Through every Middlesex, village and farm, </Para> <Para classification="secret"> For the country folk to be up and to arm. </Para></Document>
The context node is selected, which is Document. Thus the result is Document and its content.
![Page 29: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/29.jpg)
29
Select all Para Descendents
//Para
The double slash means “descendents.”
![Page 30: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/30.jpg)
30
Result
<Para classification="unclassified"> One if by land, two if by sea;</Para><Para classification="confidential"> And I on the opposite shore will be, Ready to ride and spread the alarm</Para><Para classification="unclassified"> Ready to ride and spread the alarm Through every Middlesex, village and farm,</Para><Para classification="secret"> For the country folk to be up and to arm.</Para>
All Para elements that descend from / are selected.
![Page 31: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/31.jpg)
31
Select the first Para
//Para[1]
![Page 32: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/32.jpg)
32
Result
<Para classification="unclassified"> One if by land, two if by sea;</Para>
The first Para element is selected.
![Page 33: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/33.jpg)
33
Select the last Para
//Para[last()]
![Page 34: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/34.jpg)
34
Result
<Para classification="secret"> For the country folk to be up and to arm.</Para>
last() is an XPath function. It returns a number (in our example it returns 4). Thus the fourth (last) Para element is selected.
![Page 35: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/35.jpg)
35
Select the classification attribute of the first Para
//Para[1]/@classification
![Page 36: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/36.jpg)
36
Result
unclassified
The @ symbol means “attribute.” Thus, the value of the classification attribute, of the first Para element, is selected.
![Page 37: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/37.jpg)
37
Is the Document element’s classification top-secret?
/Document/@classification = 'top-secret'
The XPath expression uses the equality comparison operator.
![Page 38: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/38.jpg)
38
Result
false
![Page 39: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/39.jpg)
39
=<>!=<=>=
Comparison Operators
Remember to escape the reserved characters: Instead of < use <
Instead of > use >Instead of <= use <=Instead of >= use >=
![Page 40: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/40.jpg)
40
Is the Document element’s classification top-secret or
secret?
(/Document/@classification = 'top-secret') or (/Document/@classification='secret')
The XPath expression uses the boolean “or” operator.
![Page 41: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/41.jpg)
41
Result
true
![Page 42: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/42.jpg)
42
A or BA and Bnot(A)
Boolean Operators
![Page 43: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/43.jpg)
43
Select all Para’s with a secret classification
//Para[@classification = 'secret']
![Page 44: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/44.jpg)
44
Result
<Para classification="secret"> For the country folk to be up and to arm.</Para>
![Page 45: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/45.jpg)
45
Check that no Para has a top-secret classification
not(//Para[@classification = 'top-secret'])
![Page 46: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/46.jpg)
46
Result
true
![Page 47: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/47.jpg)
47
Select the Para’s that don’t have a top-secret classification
//Para[@classification != 'top-secret']
![Page 48: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/48.jpg)
48
Result
<Para classification="unclassified"> One if by land, two if by sea;</Para><Para classification="confidential"> And I on the opposite shore will be, Ready to ride and spread the alarm</Para><Para classification="unclassified"> Ready to ride and spread the alarm Through every Middlesex, village and farm,</Para><Para classification="secret"> For the country folk to be up and to arm.</Para>
None of the Para elements have a top-secret classification.Do Lab1
![Page 49: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/49.jpg)
49
Establish a New Context Node
Make the second Para the context node
![Page 50: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/50.jpg)
50
Select the Following Siblings
following-sibling::*
![Page 51: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/51.jpg)
51
Result
<Para classification="unclassified"> Ready to ride and spread the alarm Through every Middlesex, village and farm,</Para><Para classification="secret"> For the country folk to be up and to arm.</Para>
These Para elements are siblings (of the second Para element) and they follow, i.e., they are following siblings.
![Page 52: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/52.jpg)
52
Select the FirstFollowing Sibling
following-sibling::*[1]
![Page 53: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/53.jpg)
53
Result
<Para classification="unclassified"> Ready to ride and spread the alarm Through every Middlesex, village and farm,</Para>
![Page 54: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/54.jpg)
54
Add Another Element
Add this <Test> element after the last Para
![Page 55: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/55.jpg)
55
Set the Context Node
Make the second Para the context node
![Page 56: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/56.jpg)
56
Select the Following Para Siblings
following-sibling::Para
![Page 57: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/57.jpg)
57
Result
<Para classification="unclassified"> Ready to ride and spread the alarm Through every Middlesex, village and farm,</Para><Para classification="secret"> For the country folk to be up and to arm.</Para>
Although the Test element is a following sibling of Para[2], it is not selected because the XPath expression selects only the Para following siblings.
![Page 58: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/58.jpg)
58
Select all Following Siblings
following-sibling::*
![Page 59: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/59.jpg)
59
Result
<Para classification="unclassified"> Ready to ride and spread the alarm Through every Middlesex, village and farm,</Para><Para classification="secret"> For the country folk to be up and to arm.</Para><Test>Hello World</Test>
The * symbol matches (selects) any element node.
![Page 60: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/60.jpg)
60
Select the First Following Para Sibling
following-sibling::Para[1]
![Page 61: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/61.jpg)
61
Result
<Para classification="unclassified"> Ready to ride and spread the alarm Through every Middlesex, village and farm,</Para>
![Page 62: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/62.jpg)
62
Select all Preceding Siblings
preceding-sibling::*
![Page 63: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/63.jpg)
63
Result
<Para classification="unclassified"> One if by land, two if by sea;</Para>
Para[2] has only one preceding sibling element node.
![Page 64: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/64.jpg)
64
Establish a New Context Node
Click on Document to make it thecontext node.
![Page 65: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/65.jpg)
65
Equivalent!
Para[1]
child::Para[1]
The first XPath expression is called the abbreviated notation. The second XPath expression is called the long notation.
![Page 66: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/66.jpg)
66
Make Para[2] the context
Establish this as the context node.
![Page 67: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/67.jpg)
67
Get parent element's classification
../@classification
Means go up one level (to the parent).
![Page 68: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/68.jpg)
68
Equivalent!
../@classification
parent::*/@classification
![Page 69: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/69.jpg)
69
Recall x, y Axes from School
• The x, y axes enables us to navigate through space.
• Similarly, we want to navigate through an XML document. So we need some axes for navigating through XML documents.
• We need a much richer set of axes than the x, y axes. We need axes that enables us to navigate to child nodes, to all descendent nodes, to the parent node, to all ancestor nodes, etc.
![Page 70: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/70.jpg)
70
List of Axis
• ancestor: use to select all ancestors
• ancestor-or-self: use to select the current node plus all its ancestors
• attribute: use to select attributes
• child: use to select child nodes
• descendant: use to select all the descendants
• descendant-or-self: use to select the current node plus all its descendants
• following: use to select everything in the document that follows the current node
• following-sibling: use to select the siblings that follow
• namespace: use to select the namespaces that are in scope
• parent: use to select the parent
• preceding: use to select everything in the document that precedes the current node
• preceding-sibling: use to select the siblings that precede
• self: use to select the current node
![Page 71: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/71.jpg)
71
Load this XML Document into Oxygen
<?xml version="1.0"?><FitnessCenter> <Member level="platinum"> <Name>Jeff</Name> <Phone type="home">555-1234</Phone> <Phone type="work">555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> </Member></FitnessCenter>
This is in the example000 folder, the file is FitnessCenter.xml
![Page 72: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/72.jpg)
72
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementName
ElementPhone
ElementPhone
ElementFavoriteColor
TextJeff
Text555-1234
Text555-4321
Textlightgrey
ancestors
precedingsibling
followingsiblings
If the context node is the Member's first phone element then: we can select all ancestors by: all preceding siblings by: all following siblings by: ancestor::* preceding-sibling::* following-sibling::* Which yields: Which yields: Which yields: Member Name Phone FitnessCenter FavoriteColor
![Page 73: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/73.jpg)
73
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementName
ElementPhone
ElementPhone
ElementFavoriteColor
TextJeff
Text555-1234
Text555-4321
Textlightgrey
descendants
If the context node is the FitnessCenter element then: we can select all descendants by: descendant::* Which yields: Member Name Phone, etc.
![Page 74: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/74.jpg)
74
Using an Axis
axis::node
Either the name of an element or the * symbol
Examples: ancestor::* child::Para
![Page 75: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/75.jpg)
75
Reload this Document
Let’s go back to this document.
![Page 76: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/76.jpg)
76
Common Error
• Distinguish between these two:child::Parachild:Para
• The first one has two colons, the second has only one.
• When specifying an axis you must follow it with two colons. (Otherwise the XPath processor will think that you’re using a namespace!)
![Page 77: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/77.jpg)
77
Default Axis
Para[1]
child::Para[1]
“child” is the default axis, i.e., if an XPath expression doesn’t specify an axis (see the first expression above) then, by default, the child axis is used.
Do Lab2
![Page 78: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/78.jpg)
78
XPath Functions
• Thus far we have been creating XPath expressions that select nodes and compare nodes in the XML document.
• The following slides show XPath expressions that operate on nodes in the XML document. This is accomplished using XPath functions.
![Page 79: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/79.jpg)
79
Count the number of Para elements
count(//Para)
Result: 4
![Page 80: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/80.jpg)
80
Count the number of Para elements with secret
classification
count(//Para[@classification = 'secret'])
Result: 1
![Page 81: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/81.jpg)
81
Add SCRIPT to the First Para
Add this word
![Page 82: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/82.jpg)
82
Does the first Para element contain the string “SCRIPT”?
contains(//Para[1], 'SCRIPT')
Result: true
![Page 83: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/83.jpg)
83
6 Kinds of Nodes
• An XML document contains these kinds of nodes:– Element nodes
– Attribute nodes
– Text nodes
– Document node
– Comment nodes
– Processing instruction nodes
• The node() function matches any kind of node.
![Page 84: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/84.jpg)
84
node() vs *
• As we’ve seen, the * symbol matches (selects) element nodes.
• The node() function matches (selects) any kind of node.
![Page 85: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/85.jpg)
85
Select all nodes containing the string “SCRIPT”
//node()[contains(., 'SCRIPT')]
For each node in the XML document: If it contains SCRIPT then select (output) it.
![Page 86: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/86.jpg)
86
Document/
PI<?xml version=“1.0”?>
ElementDocument
ElementPara
ElementPara
ElementPara
TextOne if …SCRIPT
TextAnd I …
TextReady to
…
Attributeclassification=“secret”
Attributeclassification=“unclassified”
Attributeclassification=“confidential”
Attributeclassification=“unclassified”
This element node contains SCRIPT
This text node contains SCRIPT
This element node contains SCRIPT
![Page 87: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/87.jpg)
87
Result<Document classification="secret"> <Para classification="unclassified"> One if by land, two if by sea;SCRIPT </Para> <Para classification="confidential"> And I on the opposite shore will be, Ready to ride and spread the alarm </Para> <Para classification="unclassified"> Ready to ride and spread the alarm Through every Middlesex, village and farm, </Para> <Para classification="secret"> For the country folk to be up and to arm. </Para></Document><Para classification="unclassified"> One if by land, two if by sea;SCRIPT</Para>One if by land, two if by sea;SCRIPT
![Page 88: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/88.jpg)
88
Count the number of nodes containing the string “SCRIPT”
count(//node()[contains(., 'SCRIPT')])
Result: 3
![Page 89: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/89.jpg)
89
Select the first 20 characters of the first Para
substring(//Para[1], 1, 20)
Result: cr One if by
carriage return
10 spaces
![Page 90: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/90.jpg)
90
substring(string, num1, num2?)
• The first two arguments of the substring function are mandatory. The third argument is optional.
• If the third argument is omitted then the function will return all the characters from num1 to the end of the string.
substring(//Para[1], 10) returns all characters from character 10 onward
![Page 91: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/91.jpg)
91
What's the length of the content of the first Para?
string-length(//Para[1])
Result: 52
![Page 92: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/92.jpg)
92
What's the name of the last child element of Document?
name(/Document/*[last()])
Result: Test
![Page 93: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/93.jpg)
93
translate(/Document/@classification, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
Convert Document’s classification to uppercase
Result: SECRET
![Page 94: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/94.jpg)
94
translate(string, from pattern, to pattern)
• The translate function has three arguments:– A value to be translated– A set of characters to convert from– A set of characters to convert to
• Each character in string is examined. If that character is in from pattern then it is changed to the character at the same position in to pattern. If that character is not in from pattern then it is output unchanged.
![Page 95: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/95.jpg)
95
Add a new <Cost> element
Add this element and establishDocument as the context node.
![Page 96: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/96.jpg)
96
Multiply Cost by 2
Cost * 2
Result: 24
![Page 97: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/97.jpg)
97
N mod X = the remainder of dividing N by X
Cost mod 2
Result: 0
![Page 98: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/98.jpg)
98
Arithmetic Operators
*mod- (must leave space on either side)+div
Do Lab3
![Page 99: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/99.jpg)
99
Load this XML Documentinto Oxygen
<?xml version="1.0"?><FitnessCenter> <Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor> </Member> <Member> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor> </Member> <Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor> </Member> <Employee> <Name>Erika</Name> </Employee> <Employee> <Name>Linda</Name> </Employee> <CEO> <Name>Jack</Name> </CEO> <VP> <Name>Dora</Name> </VP></FitnessCenter>
See FitnessCenter_v2.xml in the example000 folder.
![Page 100: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/100.jpg)
100
Establish FitnessCenter as the Context Node
Click on this to make it the context node.
![Page 101: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/101.jpg)
101
3 Forms of XPath Expressions
• Path Expression: expression1/expression2– Example: Member/Name
• Predicate Expression: expression1[expression2]– Example: Member[Name]
• Union Expression: expression1|expression2– Example: Member/Name|Member/Age
– Note: the "|" operator computes the union of its operands, which must be node sequences.
![Page 102: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/102.jpg)
102
Context Item
• Consider this path expression: Member/Name, and this predicate expression: Member[Name].
• In these expressions Name is evaluated once for each item in the sequence that results from evaluating Member.
<Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member><Member> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor></Member><Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>
![Page 103: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/103.jpg)
103
Context Item
• Evaluating the expression "Member" produces this sequence of element nodes:
<Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member>
<Member> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor></Member>
<Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>, ,
"Name" is evaluated once for eachitem in this sequence. Each of theseMember nodes is the context itemfor evaluating Name.
Defn: The context item is the item (node) that provides the context for evaluating an inner expression.
![Page 104: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/104.jpg)
104
Meaning of "/"
• Consider this path expression: Member/Name
• The "/" means: "for each Member node, return the result of evaluating Name".
<Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member><Member> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor></Member><Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>
Member/Name <Name>Jeff</Name>
<Name>David</Name>
<Name>Roger</Name>
![Page 105: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/105.jpg)
105
Meaning of "/" (concluded)
• Here is the general form for using "/"expression1/expression2
Each of these expressions must evaluate toa node, or sequence of nodes.Consequently, this is illegal:
Member/last()
This evaluates to an integer, not a node.
![Page 106: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/106.jpg)
106
Meaning of […]
• Consider this predicate expression: Member[Name]• The predicate [Name] means: evaluate the expression "Name"
within the context node. If Name exists then return true, else false.
• Thus, Member[Name] says: "give me all Member nodes that has a Name child".
<Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member><Member> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor></Member><Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>
Member[Name]
<Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member>
<Member> <Name>David/Name> <FavoriteColor>lighblue</FavoriteColor></Member>
<Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>
![Page 107: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/107.jpg)
107
Meaning of […] (concluded)
• Here is the general form for using […]expression1[expression2]
This expression must evaluate toa node or node sequence.
Consequently, all of these are legal: Member[true()]
Member[false()] Member[1 = 1]
Member[1 = 2]Member[Age]
This expression must evaluate toa boolean value (true or false).
These will select all Member elements
These will select no Member elements
This will select only the Member elements thathave an Age child element.
![Page 108: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/108.jpg)
108
Meaning of "|"
• Consider this union expression: expression1|expression2
• The union expression means, "create a sequence of nodes composed of the nodes selected by expression1, unioned with the nodes selected by expression2.
<Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member><Member> <FavoriteColor>lightblue</FavoriteColor></Member><Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>
Member[1]|Member[3]
<Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member>
<Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>
![Page 109: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/109.jpg)
109
Context Position
• Recall that an XPath expression returns a sequence of items.
• Consider one of those items in the sequence. Its context position is the position of the item in the sequence.
![Page 110: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/110.jpg)
110
The context position is returned by the function position()
Member[position() = 2] “Evaluate the expression: MemberIt yields a sequence of 3 Member nodes.Give me the one at position 2.”
Result:<Member> <Name>David/Name> <FavoriteColor>lighblue</FavoriteColor></Member>
![Page 111: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/111.jpg)
111
Equivalent!
Member[position() = 2]
Member[2]
The latter is a shorthand for the former.
![Page 112: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/112.jpg)
112
Context Size
• Recall that an expression returns a sequence of items.
• The context size tells you the number of items in the sequence.
![Page 113: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/113.jpg)
113
The context size is returned by the expression last()
Member[last()] “Evaluate the expression: MemberIt yields a sequence of Member nodes.Give me the last one.”
Result:<Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>
![Page 114: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/114.jpg)
114
Axis
• There are many ways that you might want to navigate through an XML tree. For example, from a context node you might want to: – navigate to a child node – navigate to an ancestor node– navigate to a preceding sibling node– navigate to a descendant node, etc.
• An axis is provided for each way that you might want to navigate through an XML tree.
• The syntax for using an axis is: axis::node
![Page 115: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/115.jpg)
115
Categories of Axes• These are axes used for navigating forward in the XML
tree:– child– descendant– attribute– self– descendant-or-self– following-sibling– following
• These are axes used for navigating backward in the XML tree:
– parent– preceding-sibling– preceding– ancestor– ancestor-or-self
![Page 116: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/116.jpg)
116
Child Axis
• The child axis is used to navigate to a child node from the context node.
./child::Member[1]/child::Name
Starting from the context node, navigate toa child node - the first Member. Now, usingthat Member as the context node, navigateto a child node - the Name node.
![Page 117: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/117.jpg)
117
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
child::Member[1]context node (.)
child::Name
![Page 118: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/118.jpg)
118
The expression "*" means any element node
./child::Member[1]/child::*
Starting from the context node, navigate toa child node - the first Member. Now, usingthat Member as the context node, navigateto all child element nodes - the Name nodeand the FavoriteColor node.
![Page 119: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/119.jpg)
119
Don't forget to use two colons
• Remember, it is child::Member
• It is NOT child:Member– The XSLT processor will think that you mean a
Member element in a "child namespace"!
![Page 120: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/120.jpg)
120
Descendant Axis
• The descendant axis is used to navigate to a node that is "under" the context node. The node could be a child node, a grandchild, a great-grandchild, etc.
./descendant::Name[1]
Starting from the context node, navigate to a descendant node - the first Name descendant. (Note: descendants of a node are considered in document order. Thus, this example says to navigate, from the context node, to the first Name element in the document.)
![Page 121: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/121.jpg)
121
Load this XML Documentinto Oxygen
<?xml version="1.0"?><FitnessCenter> <Member level="platinum"> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor> </Member> <Member level="gold"> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor> </Member> <Member level="platinum"> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor> </Member> <Employee> <Name>Erika</Name> </Employee> <Employee> <Name>Linda</Name> </Employee> <CEO> <Name>Jack</Name> </CEO> <VP> <Name>Dora</Name> </VP></FitnessCenter>
See FitnessCenter_v3.xml in the example000 folder.
![Page 122: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/122.jpg)
122
Establish the Context Node
Make this the context node.
![Page 123: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/123.jpg)
123
Attribute Axis
• The attribute axis is used to navigate to an attribute of the context node.
./child::Member[1]/attribute::level
Starting from the context node, navigate toa child node - the first Member. Now, usingthat Member as the context node, navigateto an attribute node - the level attribute.Note: if you want to select all attributes then use: attribute::*
![Page 124: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/124.jpg)
124
Self Axis
• The self axis is used to select the current node (i.e., “here”).
./child::*[not(self::Member)]/child::Name
Starting from the context node, navigate toall child element nodes that are not Membernodes. From each of the selected nodes,navigate to a child node - Name.This XPath expression will navigate to theName nodes containing: Erika, Linda, Jack,Dora.
![Page 125: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/125.jpg)
125
Incorrect
./child::*[not(Member)]/child::Name
This says: given the context node, determine if there is not a Member node within it.This XPath expression will navigate to theName nodes containing: Jeff, David, Roger,Erika, Linda, Jack, Dora.
![Page 126: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/126.jpg)
126
Equivalent!
./*[not(Member)]/Name ./child::*[not(child::Member)]/child::Name
Both of these expressions say: starting at the context node go to allchild element nodes that do not have a Member child. Then, usingthe elements selected, go to the child Name node.
The first uses the abbreviated XPath notation. The second uses the expanded XPath notation. Here we see that the expanded notation reduces ambiguity!
Do Lab4
![Page 127: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/127.jpg)
127
Descendant-or-self Axis
• This axis is used to navigate to the current node and all its descendants.
descendant-or-self::*
Give the context node and all ofthe element nodes that are under it.
![Page 128: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/128.jpg)
128
Following-sibling Axis
• This axis is used to navigate to a node that is "parallel" to the context node (i.e., at the same hierarchy level) and follows the context node.
./child::Member[1]/following-sibling::Employee[2]
Starting from the context node, navigate toa child node - the first Member. Now, usingthat Member as the context node, navigateto the second Employee sibling that follows theMember node.
<Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member><Member> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor></Member><Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member><Employee> <Name>Erika</Name></Employee><Employee> <Name>Linda</Name></Employee><CEO> <Name>Jack</Name></CEO><VP> <Name>Dora</Name></VP>
![Page 129: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/129.jpg)
129
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
context node (.)
following-sibling
Note: a node is a following sibling of another node only if it has the same parent.Consquently, the Name and FavoriteColor nodes in Member[2] and Member[3]are not following-siblings of the Name in Member[1].
![Page 130: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/130.jpg)
130
Following Axis
• This axis is used to navigate to the nodes that follow the context node.
./child::Member[1]/following::Name
Starting from the context node, navigate toa child node - the first Member. Now, usingthat Member as the context node, navigateto all the Name nodes that follow.This XPath expression will navigate to theName nodes containing: David, Roger.
![Page 131: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/131.jpg)
131
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
child::Member[1]
following nodes
context node (.)
![Page 132: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/132.jpg)
132
Document/
PI<?xml version=“1.0”?>
ElementA
ElementB
ElementC
ElementD
ElementE
Text
Text
ElementH
Text
ElementI
ElementJ
Text TextElementF
ElementG
Text
Context node
following nodes
![Page 133: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/133.jpg)
133
<?xml version="1.0"?><A> <B> <D>...</D> <E> <F>…</F> <G>…</G> </E> <H>...</H> </B> <C> <I>…</I> <J>…</J> </C></A>
following::E
![Page 134: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/134.jpg)
134
Parent Axis
• This axis is used to navigate to the parent of the context node.
./parent::FitnessCenter
Starting from the context node, navigate tothe parent, provided it is <FitnessCenter>.Suppose that we wanted to navigate to theparent, whatever it is. We would use this: parent::*. Note: if the context node is an attribute node, then its parent is the element node that the attribute is attached to.
Suppose that Member[1] is the context node
![Page 135: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/135.jpg)
135
Preceding-sibling axis
• This axis is used to navigate to a node that is "parallel" to the context node (i.e., at the same hierarchy level) and precedes the context node.
./child::*[last()]/preceding-sibling::Member[1]
Starting from the context node, navigate toa child node - the last element node. Now, usingthat node as the context node, navigateto the first Member sibling that precedes it.
<Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member><Member> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor></Member><Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member><Employee> <Name>Erika</Name></Employee><Employee> <Name>Linda</Name></Employee><CEO> <Name>Jack</Name></CEO><VP> <Name>Dora</Name></VP>
![Page 136: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/136.jpg)
136
Preceding Axis
• This axis is used to navigate to all the nodes that come before the context node.
<Member level="platinum"> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member><Member level="gold"> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor></Member><Member level="silver"> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>
./child::Member[last()]/preceding::Name
Starting from the context node, navigate toa child node - the last Member. Now, usingthat Member as the context node, navigateto all the Name nodes that precede.This XPath expression will navigate to theName nodes containing: David, Jeff.
![Page 137: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/137.jpg)
137
Load this XML document into Oxygen
<?xml version="1.0"?><Book> <Title>Origin of Wealth</Title> <Chapter> <Title>The Paradigm Shift</Title> <Section> <Title>The Question</Title> <Para>I sat perched on …</Para> </Section> </Chapter></Book>
Make this the context node
This is Book.xml in the example000 folder
![Page 138: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/138.jpg)
138
Ancestor Axis
• This axis is used to navigate to an ancestor node.
ancestor::*[2]/child::Title
Navigate to the grandparentand from there navigate to thechild Title node.
<Title>The Paradigm Shift</Title>
Result:
![Page 139: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/139.jpg)
139
Ancestor-or-self Axis
• This axis is used to navigate to the current node and its ancestors.
ancestor-or-self::*
Select the current node and all its ancestors
![Page 140: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/140.jpg)
140
text() is a function that selects text nodes
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
Text"cr "
Text"cr "
Text"cr"
<Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member>
Suppose that the context node is the Member element. Then this expression: text()will result in selecting the 3 child text nodes.
Note: cr = carriage return
![Page 141: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/141.jpg)
141
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
/FitnessCenter/Member[1]/Name selects
![Page 142: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/142.jpg)
142
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementFavoriteColor
TextJeff
Textlightgrey
ElementName
ElementFavoriteColor
TextDavid
Textlightblue
ElementName
ElementFavoriteColor
TextRoger
Textlightyellow
/FitnessCenter/Member[1]/Name/text() selects
![Page 143: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/143.jpg)
143
node() is a function that selectstext, element, document, comment and PI nodes
ElementMember
ElementName
CommentThis is a comment
TextJeff
Text"cr "
Text"cr "
Text"cr "
<Member> <Name>Jeff</Name> <!-- This is a comment --> <?bar foo="blah"?></Member>
Suppose that the context node is the Member element. Then this expression: node() will result in selecting the 4 child text nodes, the 1 element node, the 1 comment node, and the 1 PI node.
PIfoo="blah"
Text"cr"
![Page 144: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/144.jpg)
144
Examples
child::para selects the para element children of the context node.child::* selects all the element children of the context node.child::text() selects all text node children of the context node.child::node() selects all the children of the context node, whatever their node type (i.e., text node, element node, document (/) node, comment node, or processing instruction node)attribute::name selects the name attribute of the context node.attribute::* selects all the attributes of the context node.parent::node() selects the parent of the context node. If the context node is an attribute node, this expression returns the element node to which the attribute is attached.descendant::para selects all the para element descendants of the context node.ancestor::div selects all div ancestors of the context node.ancestor-or-self::div selects the div ancestors of the context node and, if the context node is a div element, the context node as well.descendant-or-self::para selects the para element descendants of the context node and, if the context node is a para element, the context node as well.self::para selects the context node if it is a para element, and otherwise nothing.
![Page 145: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/145.jpg)
145
Expanded Notation
• The expanded notation has several advantages:– Nonambiguous: it makes it very clear how you are
navigating through the XML tree.
– Powerful: it provides the means to navigate the XML tree in pretty much every way that you might desire.
• But it has a disadvantage:– Verbose: your XPath expressions can get pretty long,
even for simple things.
![Page 146: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/146.jpg)
146
Abbreviated Notation
• We will now look at the abbreviated notation.• Here are its advantages:
– Brevity: the XPath expressions are much shorter.
• Here are its disadvantages:– Ambiguous: sometimes the expression can be misinterpreted,
such as we saw with this expression: ./*[not(Member)]/Name– Less powerful: there are some things that you simply can't
do with the abbreviated notation, e.g., navigate to ancestors, navigate to preceding siblings, navigate to following siblings.
![Page 147: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/147.jpg)
147
Omitting the Axis
• Recall that when using the expanded notation you provide 3 things:– An axis (e.g., child, ancestor, descendant)– A pair of colons (i.e., ::)– A node name (or "*" if you wish to navigate to all
element or attribute nodes)
• That is, here is the general form: axis::node
• The abbreviated notation allows you to omit the axis and colons. What is the default axis?
![Page 148: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/148.jpg)
148
Default Axis
• The default axis is child. Thus, these forms are equivalent:
./child::*[not(Member)]/child::Name ./child::*[not(child::Member)]/child::Name
No axis is specified. Consequently, an XPath processorwill use the default axis: child. Thus, it will treat this as:child::Member
Member[Name] child::Member[child::Name]is equivalent to:
![Page 149: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/149.jpg)
149
Examples of using the Abbreviated Notation and the Default Axis
./child::Member[1]/child::Name ./Member[1]/Name
expanded notation abbreviated notation
./child::*[not(child::Member)]/child::Name ./*[not(Member)]/Name
expanded notation abbreviated notation
![Page 150: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/150.jpg)
150
@ is the abbreviated notation for the attribute axis
./child::Member[1]/attribute::level ./Member[1]/@level
expanded notation abbreviated notation
Do Lab5
![Page 151: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/151.jpg)
151
Load this XML Document into Oxygen
<?xml version="1.0"?><Book> <para>paragraph #1</para> <para>paragraph #2</para> <para>paragraph #3</para> <Chapter> <para>paragraph #4</para> <para>paragraph #5</para> <Section> <para>paragraph #6</para> </Section> </Chapter></Book>
Make this the context node
This is Book_v2.xml in the example000 folder
![Page 152: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/152.jpg)
152
// is the abbreviated notation for descendant-or-self::node()/
./descendant-or-self::node()/child::para.//para
expanded notationabbreviated notation
These expressions both say: starting from the context node select allthe descendant nodes as well as the context node. Now, for each ofthem select the child para elements. In other words, this results inselecting all para elements that descend from the context node.
![Page 153: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/153.jpg)
153
What will be the result?
<?xml version="1.0"?><Book> <para>paragraph #1</para> <para>paragraph #2</para> <para>paragraph #3</para> <Chapter> <para>paragraph #4</para> <para>paragraph #5</para> <Section> <para>paragraph #6</para> </Section> </Chapter></Book>
Result:<para>paragraph #1</para><para>paragraph #2</para><para>paragraph #3</para><para>paragraph #4</para><para>paragraph #5</para><para>paragraph #6</para>
./descendant-or-self::node()/child::para
![Page 154: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/154.jpg)
154
What will be the result?
<?xml version="1.0"?><Book> <para>paragraph #1</para> <para>paragraph #2</para> <para>paragraph #3</para> <Chapter> <para>paragraph #4</para> <para>paragraph #5</para> <Section> <para>paragraph #6</para> </Section> </Chapter></Book>
.//para[1]
Result:<para>paragraph #1</para><para>paragraph #4</para><para>paragraph #6</para>
![Page 155: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/155.jpg)
155
Let's see why
descendant-or-self::node()/child::para[1].//para[1]
Select all nodes that descend from the context node,including the context node. For each of them, selectthe first para child element.
is equivalent to:
![Page 156: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/156.jpg)
156
Contrast with this!
<?xml version="1.0"?><Book> <para>paragraph #1</para> <para>paragraph #2</para> <para>paragraph #3</para> <Chapter> <para>paragraph #4</para> <para>paragraph #5</para> <Section> <para>paragraph #6</para> </Section> </Chapter></Book>
./descendant::para[1]
Result:<para>paragraph #1</para>
![Page 157: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/157.jpg)
157
Let's see why
./descendant::para[1]
Select all para elements that descend from the context node. Then select the first one.
![Page 158: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/158.jpg)
158
Lesson Learned
.//para[1] ./descendant::para[1]
Not equivalent!
Do Lab6
![Page 159: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/159.jpg)
159
.. is an abbreviation of parent::node()
parent::node()..
expanded notationabbreviated notation
parent::node()/child::para../para
expanded notationabbreviated notation
![Page 160: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/160.jpg)
160
. is an abbreviation of self::node()
. self::node()
expanded notationabbreviated notation
![Page 161: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/161.jpg)
161
Good Example<Member level="platinum"> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member><Member level="gold"> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor></Member><Member level="platinum"> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>
Member[FavoriteColor='lightblue']
<Member level="gold"> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor></Member>
![Page 162: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/162.jpg)
162
Let's see its Expanded Notation
Member[FavoriteColor='lightblue']
child::Member[child::FavoriteColor='lightblue']
Select all Member child elements which have a FavoriteColorchild element whose value is lightblue.
![Page 163: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/163.jpg)
163
Examplespara selects the para element children of the context node.* selects all element children of the context node.text() selects all text node children of the context node.@name selects the name attribute of the context node.@* selects all the attributes of the context node.para[1] selects the first para child of the context node.para[last()] selects the last para child of the context node.*/para selects all para grandchildren of the context node./book/chapter[5]/section[2] selects the second section of the fifth chapter of the book whose parent is the document node.chapter//para selects the para element descendants of all the chapter element children of the context node.//para selects all the para element descendants in the document//@version selects all the version attribute nodes in the document//list/member selects all the child member elements of all the list elements..//para selects all para element descendants of the context node... selects the parent of the context node.../@lang selects the lang attribute of the parent of the context node.para[@type='warning'] selects all para children of the context node that have a type attribute with value warning.chapter[title='Introduction'] selects the chapter children of the context node that have one or more title children whose value equals Introduction.chapter[title] selects the chapter children of the context node that have one or more title children.employee[@secretary and @assistant] selects all the employee children of the context node that have both a secretary attribute and an assistant attribute.Member[Name='Jeff' or Name='Roger']/FavoriteColor selects all FavoriteColor children of Member elements that have a Name child element equal to Jeff or Roger.
![Page 164: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/164.jpg)
164
Summary
Expanded Notation Abbreviated Notationchild::para paradescendant::paraattribute::level @levelself::node() .descendant-or-self::node()/child::para .//parafollowing-sibling::parafollowing::nameparent::node() ..preceding-sibling::Memberpreceding::Nameancestor::FitnessCenterancestor-or-self::Title
![Page 165: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/165.jpg)
165
Predicates
• A predicate consists of an expression, called a predicate expression, enclosed in square brackets.
• A predicate serves to filter a sequence--retaining some items and discarding others.
• Those items for which the predicate evaluates to true are retained, and those for which the predicate evaluates to false are discarded.
![Page 166: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/166.jpg)
166
Predicates
path-expression[predicate-expression]
The predicate-expressionis evaluated within the contextprovided by the path-expression.The result of evaluating thepredicate expression is a boolean value:true or false.
![Page 167: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/167.jpg)
167
Numeric Predicates
• If the predicate expression is a number, then the predicate evaluates to true if the number equals the context position.
• Consider this XPath: Member[2]
• The path expression yields this sequence:
<Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member>
<Member> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor></Member>
<Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>, ,
The predicate is evaluated against each of these items. Since the predicate is a number it will becompared against each item's context position. For the first item its context position is 1 and 1 = 2so the first item is discarded. For the second item its context position is 2 and 2 = 2 so the second itemis retained. For the third item its context position is 3 and 3 = 2 so the third item is discarded.
![Page 168: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/168.jpg)
168
Element Predicates
• If the predicate expression is an element node, then the predicate evaluates to true if the node exists. (Conversely, a predicate expression which evaluates to an empty sequence evaluates to false)
• Consider this predicate expression: Member[FavoriteColor]• Suppose the Member path expression yields this sequence:
<Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member>
<Member> <Name>David</Name></Member>
<Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>, ,
Note that the second Member does not have a FavoriteColor. The predicate is evaluated against each of these items. Since the predicate is an element node the XSLT processor will determine ifthe node exists for each item. For the first Member there does exist a FavoriteColor child element soit is retained. For the second Member there does not exist a FavoriteColor child element so it isdiscarded. For the third Member there does exist a FavoriteColor child element so it is retained.
![Page 169: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/169.jpg)
169
Boolean Predicates
• If the predicate expression is a boolean expression, then the predicate evaluates to true if the boolean expression evaluates to true.
• Consider this XPath: Member[Name = 'Jeff']• Suppose the Member path expression yields this
sequence:<Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor></Member>
<Member> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor></Member>
<Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>, ,
The predicate is evaluated against each of these items. The first Member has a child Name elementwith a value equal to Jeff, so it is retained. The second and third Member elements have a child Nameelement, but with a value other than Jeff, so they are discarded.
![Page 170: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/170.jpg)
170
Multiple Predicates
• It is legal to have multiple predicates:
path-expression[predicate-expression1] [predicate-expression2]
{sequence of nodes selected by path-expression}
filter using predicate-expression1
{sequence of nodes after filtering with predicate-expression1}
filter using predicate-expression2
{sequence of nodes after filtering with predicate-expression2}
• They are evaluated as follows:
![Page 171: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/171.jpg)
171
<?xml version="1.0"?><FitnessCenter> <Member level="platinum"> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor> </Member> <Member level="gold"> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor> </Member> <Member level="platinum"> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor> </Member></FitnessCenter>
Member[@level='platinum'][2]
<Member level="platinum"> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor></Member>
Member[2] [@level='platinum']
-- empty --
Order Matters!
Do Lab7
![Page 172: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/172.jpg)
172
Arithmetic Expressions
• XPath provides these arithmetic operators:
+ (addition)- (subtraction and unary negation)* (multiplication)div (division)mod (modulus, i.e., the remainder from division)
Example: child::Cost * 2 (multiply the value of the Cost child element by 2)Example: position() mod 2 = 0 (this expression will evaluate to true for context items at even positions, and false otherwise)
![Page 173: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/173.jpg)
173
Boolean Operators
• XPath provides these boolean operators:
andornot
Example: child::Cost and child::Member (This expression will evaluate to true only if there exists a Cost child element
and a Member child element)Example: ((child::Cost * 2 = 50) and (child::Member))
![Page 174: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/174.jpg)
174
Parenthesized Expressions
• Use parentheses to ensure that your expressions are evaluated in the order you desire.
Example: (child::Cost and child::Member) or (child::Cost and child::Test) This expression will evaluate to true if there exists a Cost child element and a Member child element, or there exists a Cost child element and a Test child element.
![Page 175: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/175.jpg)
175
Comparison Operators
• These general comparison operators are available:
= | != | < | <= | > | >=
![Page 176: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/176.jpg)
176
Load this XML Documentinto Oxygen<?xml version="1.0"?><FitnessCenter> <Member> <Name>Jeff</Name> <Age>39</Age> </Member> <Member> <Name>David</Name> <Age>33</Age> </Member> <Member> <Name>Roger</Name> <Age>36</Age> </Member></FitnessCenter>
Make this the context node
This is FitnessCenter_v4.xml in the example000 folder.
![Page 177: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/177.jpg)
177
General Comparison
Member/Age = 39Member/Age != 20Member/Age < 40Member/Age <= 40Member/Age > 30Member/Age >= 30
<?xml version="1.0"?><FitnessCenter> <Member> <Name>Jeff</Name> <Age>39</Age> </Member> <Member> <Name>David</Name> <Age>33</Age> </Member> <Member> <Name>Roger</Name> <Age>36</Age> </Member></FitnessCenter>
Result:Member/Age = 39 … trueMember/Age != 20 … trueMember/Age < 40 … trueMember/Age <= 40 … trueMember/Age > 30 … trueMember/Age >= 30 … true
Note that I am not specifying a specific Member. Consequently, I am comparing all Member Ages.
![Page 178: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/178.jpg)
178
General Comparison<?xml version="1.0"?><FitnessCenter> <Member id="1" level="platinum"> <Name>Jeff</Name> <Phone type="home">555-1234</Phone> <Phone type="work">555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> <MembershipFee>340</MembershipFee> <Age>39</Age> </Member> <Member id="2" level="gold"> <Name>David</Name> <Phone type="home">383-1234</Phone> <Phone type="work">383-4321</Phone> <FavoriteColor>lightblue</FavoriteColor> <MembershipFee>500</MembershipFee> <Age>33</Age> </Member> <Member id="3" level="platinum"> <Name>Roger</Name> <Phone type="home">888-1234</Phone> <Phone type="work">888-4321</Phone> <FavoriteColor>lightyellow</FavoriteColor> <MembershipFee>340</MembershipFee> <Age>36</Age> </Member></FitnessCenter>
39
Member/Age = 39?
Does there exist an Age valuethat equals 39? Yes!
Member/Age = 39 is equivalent to: (Member[1]/Age = 39) or (Member[2]/Age = 39) or (Member[3]/Age = 39)
![Page 179: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/179.jpg)
179
Use General Comparison to Compare Two Sequences
<?xml version="1.0"?><FitnessCenter> <Member> <Name>Jeff</Name> <Age>39</Age> <Salary units="thousands">20</Salary> </Member> <Member> <Name>David</Name> <Age>33</Age> <Salary units="thousands">39</Salary> </Member> <Member> <Age>36</Age> <Salary units="thousands">45</Salary> </Member></FitnessCenter>
Member/Age = Member/Salary
Result: Member/Age = Member/Salary … true
"Is there is an Age valuewhich equals a Salary value."
![Page 180: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/180.jpg)
180
All Combinations of Sequence Values are Compared
Age393336
Salary203945
![Page 181: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/181.jpg)
181
XPath Functions• XPath provides a lot of functions for you to use. We have
already seen some, such as position(), last(), text(), node(), count(), and translate().
• Here's how the XPath functions are categorized:– String functions: those functions which when evaluated return a string.
– Boolean functions: those functions which when evaluated return a boolean value (true or false).
– Number functions: those functions which when evaluated return a number.
– Node functions: those functions which when evaluated return a node.
– Namespace functions: those functions that are intended for use with namespaces.
![Page 182: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/182.jpg)
182
Notation
• Some functions have optional arguments. For example, the name function has an optional argument. I will use "?" after the argument to indicate that it is optional, e.g., name(node?).
• Some functions allow zero or more occurrences of an argument. For example, the concat function allows zero or more strings after the first two arguments. I will use "*" after the argument to indicate zero or more occurrences, e.g.,
concat(string, string, string*)
![Page 183: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/183.jpg)
183
String Functions
• name(node?) - this function returns the name of a node. If no argument is provided then it returns the name of the context item.
• string(object?) - If object is a node this function returns the value of the node (if the node is not a leaf node then it concatenates the values of all the leaf nodes that are under it)
• concat(string, string, string*) - this function concatenates all its arguments. There must be at least two arguments.
Example: concat(‘Name; ’, child::Name)
![Page 184: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/184.jpg)
184
String Functions (cont.)
• substring-before(string1, string2) - this function returns a string representing the substring of string1 that precedes the first occurrence of string2, or the empty string if string1 does not contain string2.
• substring-after(string1, string2) - this function returns a string representing the substring of string1 that follows the first occurrence of string2, or the empty string if string1 does not contain string2.
Example: substring-before(child::Phone, '555')
Example: substring-after(child::Phone, '555')
![Page 185: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/185.jpg)
185
String Functions (cont.)
• substring(string, number1, number2?) - this function returns a string representing the substring of string starting at the position specified by number1 with the length specified by number2. If number2 is not specified then it returns the substring starting at the position specified by number1, and continuing to the end of string.
• translate(string1, string2, string3) - this function returns a string obtained by converting each character in string1 that is contained in string2 into the character in string3 that is at the same position as in string2. For those characters in string1 which are not present in string2, they are returned untouched.
![Page 186: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/186.jpg)
186
substring(string, num1, num2?)
Example: substring('1234567890', 2, 5) returns '23456'
![Page 187: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/187.jpg)
187
String Functions (concluded)
• normalize-space(string?) - this function returns a string obtained by trimming leading and trailing whitespaces (space, carriage return, tab, linefeed) off the argument, and replacing sequences of whitespaces by a single space. If the argument is omitted, it defaults to the context node converted to a string. In other words, these two function calls are equivalent:
normalize-space()
is equivalent to:
normalize-space(string(self::*))
![Page 188: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/188.jpg)
188
Boolean Functions
• starts-with(string1, string2) - this function returns true if string1 starts with string2, and otherwise returns false.
• contains(string1, string2) - this function returns true if string1 contains string2, and otherwise returns false.
Example: starts-with(child::Phone, '555')
Example: contains(child::FavoriteColor, 'blue')
![Page 189: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/189.jpg)
189
Boolean Functions (concluded)
• boolean(object) - this function converts object to a boolean value as follows:– if object is a number (not zero) then the function returns true, if object is zero
then the function returns false.
– if object is a node which exists then the function returns true; if the node does not exist then the function returns false.
– if object is a string of length greater than zero then the function returns true; otherwise the function returns false.
• true() - this function returns true• false() - this function returns false• lang(string) - this function returns true if the xml:lang value of the
context item matches string, and false otherwise. (Recall the default value of xml:lang is 'EN')
![Page 190: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/190.jpg)
190
Number Functions
• position() - this function returns an integer, representing the position of the context item within a sequence of items.
• last() - this function returns an integer, representing the context size.
• count(node sequence) - this function returns an integer representing the number of nodes in node sequence.
• string-length(string?) - this function returns an integer representing the number of characters in string. If the argument is omitted, it defaults to the context node converted to a string.
Example: string-length(child::FavoriteColor)
![Page 191: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/191.jpg)
191
Number Functions (concluded)
• number(object?) - this function converts object to a number as follows:– if object is a number then it returns the number.– if object is a boolean true then it returns 1, if object is a boolean false then
it returns 0.– if object is a node then it returns the value of the node.
• sum(node sequence) - this functions sums up the values of all the nodes that are identified by node sequence.
• floor(number) – truncates the number.• ceiling(number) – next integer.• round(number) - rounds the number, e.g., 12.5 rounds to 13, -12.5
rounds to -12.
![Page 192: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/192.jpg)
192
floor(), ceiling(), round()
Example: floor(2.5) returns 2
Example: ceiling(2.5) returns 3
Example: round(2.3) returns 2
![Page 193: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/193.jpg)
193
Node Functions
• text() - this function matches on any text node.
• node() - this function matches on any text, element, document, comment, or PI node.
• id(node id) - this function returns the node with an ID value equal to node id.
Example: id(child::GuestAuthor/BookForSigning/@isbn-ref) will return the element which has an ID value that matches isbn-ref.
![Page 194: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/194.jpg)
194
Open this file in Oxygen
<?xml version="1.0"?><FitnessCenter xmlns="http://www.gym.com"> <Member level="platinum"> <Name>Jeff</Name> <Phone type="home">555-1234</Phone> <Phone type="work">555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> </Member></FitnessCenter>
This is FitnessCenter_v5.xml in the example000 folder.
Establish this as the context node
![Page 195: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/195.jpg)
195
Namespace Terminology
{http://www.gym.com}Member
Expanded name = The combination of the namespace URI and the local name
Local name
Namespace URI
![Page 196: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/196.jpg)
196
Namespace Terminology (cont.)
<gym:FitnessCenter xmlns:gym="http://www.gym.com"> <gym:Member> …</gym:FitnessCenter>
<gym:Member>
prefix
![Page 197: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/197.jpg)
197
local-name(node?)
• This is an XPath function which returns a string, corresponding to the local name of the node.
local-name(.)
Result:Name
![Page 198: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/198.jpg)
198
namespace-uri(node?)
• This is an XPath function which returns a string corresponding to the namespace URI of the node.
namespace-uri(.)
Result:http://www.gym.com
![Page 199: 1 XPath 1.0 Roger L. Costello 6 March 2010](https://reader033.vdocuments.net/reader033/viewer/2022051014/55146177550346b0158b4930/html5/thumbnails/199.jpg)
199
Congratulations!
• You now know everything there is to know about XPath 1.0!
• We have covered the entire XPath specification. We have left no stone unturned.