Lecture_5
Lecture_5
book
<bookstore> <bookstore>
<book> <book>
<7title lang="en“ lang=“ar">Harry Potter</title> <title lang="en">Harry Potter</title>
<price>29.99</book></price> <price>29.99</price>
</book> </book>
<book> <book>
<title lang="en">Learning XML</title> <title lang="en">Learning XML</title>
<price>39.95</price> <price>39.95</price>
</book> </book>
</bookstore>
</bookstore>
Examples of Path Expressions in XPath
Q1: /library/author
– Addresses all author elements that are children of
the library element node immediately below root
– /t1/.../tn, where each ti+1 is a child node of ti, is a
path through the tree representation
Q2: //author
– Consider all elements in document and check
whether they are of type author
– Path expression addresses all author elements
anywhere in the document
Examples of Path Expressions in XPath
Q3: /library/@location
– Addresses location attribute nodes within library
element nodes
– The symbol @ is used to denote attribute nodes
Q4: //book/@title="Artificial Intelligence”
– Adresses all title attribute nodes within book
elements anywhere in the document that have the
value “Artificial Intelligence
Examples of Path Expressions in XPath
//book/@title="Artificial Intelligence”
Examples of Path Expressions in XPath
Q6: Address first author element node in the XML
document
//author[1]
Q7: Address last book element within the first
author element node in the document
//author[1]/book[last()]
Q8: Address all book element nodes without a title
attribute
//book[not @title]
Examples of Path Expressions in XPath
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="en">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
Examples of Path Expressions in XPath
Expression Description
@ Selects attributes
[1] Selects the first element that is the child of the element.
[last()] Selects the last element that is the child of the element
[position()<3] Selects the first two book elements that are children of the element
//element[@attribute] Selects all the elements that have a certain attribute named
@ Selects attributes
/bookstore/book[1] Selects the first book element that is the child of the bookstore
/bookstore/book[last()] Selects the last book element that is the child of the bookstore element
/bookstore/book[position()<3] Selects the first two book elements that are children of the bookstore element
//title[@lang] Selects all the title elements that have an attribute named lang