17mci0002 Suganya.D Executing X-Queries On Basex
17mci0002 Suganya.D Executing X-Queries On Basex
17mci0002 Suganya.D Executing X-Queries On Basex
SUGANYA.D
Executing X-queries on BaseX
Filename - book.xml
<bookstore>
<book category="COOKING">
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<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">
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Xquery
for $x in doc("book.xml")/bookstore/book
where $x/price>30
return $x/title
Search and Order
for $x in doc("book.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
book-titles in our bookstore in an HTML list
<ul>
for $x in doc("book.xml")/bookstore/book/title
order by $x
return <li>{$x}</li>
</ul>
<ul>
for $x in doc("book.xml")/bookstore/book/title
order by $x
return <li>{data($x)}</li>
</ul>
Xquery in if conditional expression
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>
Adding element and attributes in the result
for $x in doc("books.xml")/bookstore/book/title
order by $x
return $x
Add html element and text
<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>
Add attributes to html element
<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>
Selecting and filtering the element
for $x in doc("book.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
Using at keyword
for $x at $i in doc("book.xml")/bookstore/book/title
return <book>{$i}. {data($x)}</book>
String len function
let $courses :=
<courses>
<course>Java</course>
<course>DotNet</course>
<course>C/C++</course>
<course>Oracle</course>
</courses>
return
<results>
<courses>{
string-join($courses/course, ',')
}</courses>
</results>
Current date and time