0% found this document useful (0 votes)
38 views17 pages

17mci0002 Suganya.D Executing X-Queries On Basex

This document contains examples of using XQuery to query an XML document called book.xml that contains book data. Some examples include filtering books by price, ordering results, returning book titles in an HTML list, using IF/THEN conditional expressions, and adding HTML elements and attributes to results. Other examples demonstrate string functions like length, concatenation, and join as well as retrieving the current date and time.

Uploaded by

Akhil Venkata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views17 pages

17mci0002 Suganya.D Executing X-Queries On Basex

This document contains examples of using XQuery to query an XML document called book.xml that contains book data. Some examples include filtering books by price, ordering results, returning book titles in an HTML list, using IF/THEN conditional expressions, and adding HTML elements and attributes to results. Other examples demonstrate string functions like length, concatenation, and join as well as retrieving the current date and time.

Uploaded by

Akhil Venkata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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