XQuery FLWOR Expressions with HTML in XML

In this article we will discuss about how to generate the HTML list from XML document.
  • 2213

Now we will see how to show the result of the XQuery in the form of HTML list. Suppose we have the following FLWOR expression to extract all the title element under the Book element which further resides within the Bookshop Element. The expression is as follows

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

The above expression's output will be like this

<title>2 States</title>
 <title>JulierCeaser</title>
 <title>Revolution 2020</title>
<title>The Notebook</title>

If we want we can convert it i n the form of HTML List and we will do it by the following Expression

<ul>
{

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

The output will be like this

<ul>
   <li><title>2 States</title></li>
   <li><title>JulierCeaser</title></li>
   <li><title>Revolution 2020</title></li>
   <li><title>The Notebook</title></li>
</ul>

If we want to show only the data residing within the title element and want to remove the title element we will do the following
 

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

The output will be like this

<ul>
<li>2 States</li>
<li>JulierCeaser</li>
<li>Revolution 2020</li>
<li>The Notebook</li>
</ul>

Here we will see that only the title data is coming in terms of HTML List.

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.