XQuery HTML

Books.xml:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
 
  <book category="Child">
    <title lang="en">ABC</title>
    <author>Author Name</author>
    <year>2020</year>
    <price>100.00</price>
  </book>
 
  <book category="IT">
    <title lang="en">XQuery Book</title>
    <author>Author 1</author>
    <author>Author 2</author>
 <year>2005</year>
    <price>300.00</price>
  </book>
 
  <book category="Sociology">
    <title lang="en">Sociology 1</title>
    <author>Author Name</author>
 <year>2010</year>
    <price>250.00</price>
  </book>
 
  <book category="GK">
    <title lang="en">Current Affairs</title>
    <author>Author Name</author>
 <year>2004</year>
    <price>500.00</price>
  </book>
 
  <book category="Science">
    <title lang="en">Science Book</title>
    <author>Author 1</author>
    <author>Author 2</author>
    <author>Author 3</author>
 <year>2011</year>
    <price>150.00</price>
  </book>
 
</bookstore>

Present the Result In an HTML List:

Example 1:

for $x in doc("books.xml")/bookstore/book/title
order by $x
return $x

Explanation:

In the above example, we are using the XQuery FLWOR expression to select all the title elements under the book elements that are under the bookstore element. Here, we are returning the title elements in alphabetical order.

Example 2:

<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{$x}</li>
}
</ul>

Explanation:

In the above example, we are listing all the book-titles in the bookstore in an HTML list by adding the <ul> and <li> tags to the FLWOR expression.

Result:

<ul>
<li><title lang="en">ABC</title></li>
<li><title lang="en">XQuery Book</title></li>
<li><title lang="en">Sociology 1</title></li>
<li><title lang="en">Current Affairs</title></li>
<li><title lang="en">Science Book</title></li>
</ul>

Example 3:

<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{data($x)}</li>
}
</ul>

Explanation:

In the above example, we are eliminating the title element, to show only the data inside the title element.

Result:

<ul>
<li>ABC</title></li>
<li>XQuery Book</title></li>
<li>Sociology 1</title></li>
<li>Current Affairs</title></li>
<li>Science Book</title></li>
</ul>
Please Share