Home >>XQuery Tutorial >XQuery Add

XQuery Add

XQuery Add

XQuery Attach is used to add elements, descriptions, HTML elements, and text to input document data.

Let's understand with an example.

XQuery Add Example

Take an XML file named books.xml, having the following data.

books.xml


<bookstore>  
<book category="Mythology">  
<title lang="en">Immortals of Meluha</title>  
<author>Anshika</author>  
<year>2012</year>  
<price>150</price>  
</book>  
<book category="Children">  
<title lang="en">Max</title>  
<author>Harry</author>  
<year>2008</year>  
<price>350</price>  
</book>  
<book category="Programming">  
<title lang="en">Let's C</title>  
<author>Charlie</author>  
<year>2005</year>  
<price>250</price>  
</book>  
</bookstore>  

XQuery Expression

books.xqy


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

Add HTML Elements and Text

If you want to add some HTML elements to the result, use the XQuery like this:

book.xqy:


<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>