Xquery and Xpath 2
Xquery and Xpath 2
Xquery and Xpath 2
1. doc("books.xml")
To open the "books.xml" file
Output
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
2. doc("books.xml")/bookstore/book/title
To select all the title elements in the "books.xml" file
OUTPUT
<title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
3. doc("books.xml")/bookstore/book[price<30]
To select all the books in the bookstore that have price less than 30
OUTPUT
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
FLWOR (pronounced "flower") is an acronym for "For, Let, Where, Order by,
Return".
For - selects a sequence of nodes
Let - binds a sequence to a variable
Where - filters the nodes
Order by - sorts the nodes
Return - what to return (gets evaluated once for every node)
FOR CLAUSE
for $x in (1 to 5)
return <test>{$x} </test>
OUTPUT
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>
for $x in (10,20), $y in (100,200)
return <test>x={$x} and y={$y} </test>
OUTPUT
<test>x=10 and y=100</test>
<test>x=10 and y=200</test>
<test>x=20 and y=100</test>
<test>x=20 and y=200</test>
LET CLAUSE
let $x: = (1 to 5)
return <test>{$x} </test>
OUTPUT
<test>1 2 3 4 5</test>
WHERE CLAUSE
where $x/price>30 and $x/price<100
ORDER BY CLAUSE
for $x in doc("books.xml")/bookstore/book
order by $x/@category, $x/title
return $x/title
OUTPUT
<title lang="en">Harry Potter</title>
<title lang="en">Everyday Italian</title>
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>
RETURN CLAUSE
for $x in doc("books.xml")/bookstore/book
return $x/title
OUTPUT
<title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
4.
for $x in doc("books.xml")/bookstore/book
where $x/price>30
return $x/title
Select all the title of the book in the bookstore that have a price higher than 30.
Same as
doc("books.xml")/bookstore/book[price>30]/title
OUTPUT
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
5.
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
The for clause selects all book elements under the bookstore element into a
variable called $x.
The where clause selects only book elements with a price element with a value
greater than 30.
The order by clause defines the sort-order. Will be sort by the title element.
The return clause specifies what should be returned. Here it returns the title
elements.
OUTPUT
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>
6.
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{data($x)} </li>
}
</ul>
To list all the book-titles in our bookstore in an HTML list and display only the
data inside the title element.
OUTPUT
<ul>
<li>Everyday Italian</li>
<li>Harry Potter</li>
<li>Learning XML</li>
<li>XQuery Kick Start</li>
</ul>
CONDITIONAL STATEMENTS
7.
for $x in doc("books.xml")/bookstore/book
return if ($x/@category="CHILDREN")
then <child>{data($x/title)} </child>
else <adult>{data($x/title)} </adult>
if then else
OUTPUT
<adult>Everyday Italian</adult>
<child>Harry Potter</child>
<adult>XQuery Kick Start</adult>
<adult>Learning XML</adult>
COMPARISIONS
$bookstore//book/@q > 10
$bookstore//book/@q gt 10
The following expression returns true if any q attributes have a value greater
than 10
XPATH
XPath specifies seven types of nodes which can be the output of execution of
the XPath expression.
1. Root
2. Element
3. Text
4. Attribute
5. Comment
6. Processing Instruction
7. Namespace
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
<bookstore> (root element node)
<author>J K. Rowling</author> (element node)
lang="en" (attribute node)
Atomic values
Atomic values are nodes with no children or parent.
Example of atomic values:
J K. Rowling
"en"
Items
Items are atomic values or nodes.
Relationship of Nodes
Parent
Each element and attribute have one parent.
<bookstore>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
In this example; the book element is the parent of the title, author, year, and
price
Children
Element nodes may have zero, one or more children.
In the example; the title, author, year, and price elements are all children of the
book element:
Siblings
Nodes that have the same parent.
In the example; the title, author, year, and price elements are all siblings:
Ancestors
A node's parent, parent's parent, etc.
In the example; the ancestors of the title element are the book element and the
bookstore element:
Descendants
A node's children, children's children, etc.
In the example; descendants of the bookstore element are the book, title, author,
year, and price elements:
Selecting Nodes
XPath uses path expressions to select nodes in an XML document. The node is
selected by following a path or steps.
The most useful path expressions are listed below:
Expression Description
nodename Selects all nodes with the name "nodename"
// Selects nodes in the document from the current node that match the
selection no matter where they are
+ Addition 6+4
- Subtraction 6-4
* Multiplication 6*4
= Equal price=9.80
or or price=9.80 or
price=9.70
and and price>9.00 and
price<9.90
ABSOLUTE PATH
If location path starts with root node or with '/' then it is an absolute path.
/class/student − select student nodes within class root node
<xsl:for-each select = "/class/student">
/class/student/firstname − select firstname of a student node within class
root node
<p><xsl:value-of select = "/class/student/firstname"/></p>
Example –
students.xsl
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/" >
<html>
<body>
<h3>Details of each Students. </h3>
<table border = "1">
<tr bgcolor = "#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<tr>
<td><xsl:value-of select = "/class/student[1]/@rollno"/></td>
<td><xsl:value-of select = "/class/student[1]/firstname"/></td>
<td><xsl:value-of select = "/class/student[1]/lastname"/></td>
<td><xsl:value-of select = "/class/student[1]/nickname"/></td>
<td><xsl:value-of select = "/class/student[1]/marks"/></td>
</tr>
<tr>
<td>
<xsl:value-of select = "/class/student/@rollno"/>
</td>
<td><xsl:value-of select = "/class/student[2]/firstname"/></td>
<td><xsl:value-of select = "/class/student[2]/lastname"/></td>
<td><xsl:value-of select = "/class/student[2]/nickname"/></td>
<td><xsl:value-of select = "/class/student[2]/marks"/></td>
</tr>
<tr>
<td>
<xsl:value-of select = "/class/student[3]/@rollno"/>
</td>
<td><xsl:value-of select = "/class/student[3]/firstname"/></td>
<td><xsl:value-of select = "/class/student[3]/lastname"/></td>
<td><xsl:value-of select = "/class/student[3]/nickname"/></td>
<td><xsl:value-of select = "/class/student[3]/marks"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
RELATIVE PATH
Example locating the elements using relative path.
firstname − select firstname related to student nodes.
<p><xsl:value-of select = "firstname"/></p>
Example
students.xsl
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/" >
<html>
<body>
<h3>Details of each Students. </h3>
<table border = "1">
<tr bgcolor = "#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select = "/class/student">
<tr>
<td><xsl:value-of select = "@rollno"/></td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "nickname"/></td>
<td><xsl:value-of select = "marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XPATH WILDCARD
XPath defines the following wildcards on nodes to be used with the XPath
expressions.
S.No. WildCard & Description
1 *
Example
students.xsl
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/">
<html>
<body>
<h2>Students</h2>
<xsl:apply-templates select = "class/*" />
</body>
</html>
</xsl:template>
<xsl:template match = "class/*">
<xsl:apply-templates select = "@rollno" />
<xsl:apply-templates select = "firstname" />
<xsl:apply-templates select = "lastname" />
<xsl:apply-templates select = "nickname" />
<xsl:apply-templates select = "marks" />
<br />
</xsl:template>
<xsl:template match = "@rollno">
<span style = "font-size = 22px;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "firstname">
First Name:<span style = "color:blue;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "lastname">
Last Name:<span style = "color:green;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "nickname">
Nick Name:<span style = "color:red;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "marks">
Marks:<span style = "color:gray;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
</xsl:stylesheet>
XPATH AXES
Axes are used to identify elements by their relationship like parent, child,
sibling, etc. Axes are named so because they refer to axis on which elements are
lying relative to an element.
Following is the list of various Axis values.
1 ancestor
Represents the ancestors of the current node which include the
parents up to the root node.
2
ancestor-or-self
Represents all nodes that come before the current node (i.e. before
it's opening tag).
12
self