You are on page 1of 17

17MCI0002

SUGANYA.D
Executing X-queries on BaseX

Filename - book.xml

<?xml version="1.0" encoding="UTF-8"?>

<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>
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 $bookTitle := "Java Programming"


let $size := string-length($bookTitle)
return
<result>
<size>{$size}</size>
</result>
Concatenation of string

let $bookTitle := "Learn XQuery in 24 hours"


let $updatedTitle := concat($bookTitle,",price: 200$")
return
<result>
<title>{$updatedTitle}</title>
</result>
STRING JOIN

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

let $date := current-date()


return
<results>
<date>{$date}</date>
</results>

You might also like