Sei sulla pagina 1di 25

XQUERY

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

ADDING ELEMENTS AND ATTRIBUTES


9.
ADDING HTML ELEMENT
<html>
<body>
<h1>Bookstore</h1>
<ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li>{data($x/title)}. Category: {data($x/@category)} </li>
}
</ul>
</body>
</html>
OUTPUT
<html>
<body>
<h1>Bookstore</h1>
<ul>
<li>Everyday Italian. Category: COOKING</li>
<li>Harry Potter. Category: CHILDREN</li>
<li>Learning XML. Category: WEB</li>
<li>XQuery Kick Start. Category: WEB</li>
</ul>
</body>
</html>
10.
ADDING ATTRIBUTES
<html>
<body>
<h1>Bookstore</h1>
<ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li class="{data($x/@category)}">{data($x/title)} </li>
}
</ul>
</body>
</html>
OUTPUT
<html>
<body>
<h1>Bookstore</h1>
<ul>
<li class="COOKING">Everyday Italian</li>
<li class="CHILDREN">Harry Potter</li>
<li class="WEB">Learning XML</li>
<li class="WEB">XQuery Kick Start</li>
</ul>
</body>
</html>
XQUERY FUNCTIONS
1: In an element
<name>{upper-case($booktitle)} </name>
2: In the predicate of a path expression
doc("books.xml")/bookstore/book[substring(title,1,5) = 'Harry']
3: In a let clause
let $name: = (substring($booktitle,1,4))

XQUERY USER-DEFINED FUNCTIONS


Syntax
declare function prefix: function_name ($parameter as datatype)
as returnDatatype
{
...function code here...
};
Example
declare function local: minPrice ($p as xs:decimal? $d as xs:decimal?)
as xs:decimal?
{
let $disc: = ($p * $d) div 100
return ($p - $disc)
};

Calling the function above:


<minPrice> {local: minPrice ($book/price, $book/discount)} </minPrice>
________________________________________________________________

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 from the root node

// Selects nodes in the document from the current node that match the
selection no matter where they are

. Selects the current node

.. Selects the parent of the current node


@ Selects attributes

Some path expressions and the result of the expressions:

Path Expression Result

bookstore Selects all nodes with the name "bookstore"

/bookstore Selects the root element bookstore

Note: If the path starts with a slash ( / ) it always


represents an absolute path to an element
bookstore/book Selects all book elements that are children of
bookstore
//book Selects all book elements no matter where they are
in the document
bookstore//book Selects all book elements that are descendant of the
bookstore element, no matter where they are under
the bookstore element
//@lang Selects all attributes that are named lang

XPath Expression Example


This XML document, students.xml and its stylesheet document students.xsl uses
the XPath expressions under select attribute of various XSL tags to get the
values of roll no, firstname, lastname, nickname and marks of each student
node.
students.xml
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<class>
<student rollno = "393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno = "493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno = "593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
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>
<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 Operators
Below is a list of the operators that can be used in XPath expressions:

Operator Description Example

| Computes two node-sets //book | //cd

+ Addition 6+4

- Subtraction 6-4

* Multiplication 6*4

div Division 8 div 4

= Equal price=9.80

!= Not equal price!=9.80

< Less than price<9.80

<= Less than or equal to price<=9.80

> Greater than price>9.80

>= Greater than or equal to price>=9.80

or or price=9.80 or
price=9.70
and and price>9.00 and
price<9.90

mod Modulus (division remainder) 5 mod 2

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 *

used to match any node.


2 .

used to match the current node in


context.
3 @*

used to match any attribute


4
node()

used to match node of any type

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.

S.No. Axis & Description

1 ancestor
Represents the ancestors of the current node which include the
parents up to the root node.
2
ancestor-or-self

Represents the current node and it's ancestors.


3 attribute

Represents the attributes of the current node.


4 child

Represents the children of the current node.


5 descendant

Represents the descendants of the current node. Descendants include


the node's children upto the leaf node(no more children).
6
descendant-or-self

Represents the current node and it's descendants.


7 following

Represents all nodes that come after the current node.


8 following-sibling

Represents the following siblings of the context node. Siblings are at


the same level as the current node and share it's parent.
9 namespace

Represents the namespace of the current node.


10
parent

Represents the parent of the current node.


11 preceding

Represents all nodes that come before the current node (i.e. before
it's opening tag).
12
self

Represents the current node.


Example on the uses of axes.
firstname − select firstname related to student nodes.
<p><xsl:value-of select = "firstname"/></p>
<xsl:value-of select = "/class/student/preceding-sibling::comment()"/>
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>
<xsl:value-of select = "/class/student/preceding-sibling::comment()"/>
<br/>
<xsl:text>First Student: </xsl:text>
<xsl:value-of select = "/class/student/child::firstname" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Potrebbero piacerti anche