Xquery Html in XML
Using XQuery to Generate HTML from XML
What is XQuery HTML?
XQuery can be used to extract data from XML and generate HTML output dynamically.
This is helpful for displaying XML data in web pages.
XQuery constructs HTML elements as XML nodes and outputs them as valid HTML.
Example XML
<library> <book category="fiction"> <title>The Hobbit</title> <author>J.R.R. Tolkien</author> </book> <book category="history"> <title>Sapiens</title> <author>Yuval Noah Harari</author> </book></library>Example XQuery to Generate HTML Table
<html> <head> <title>Library Books</title> </head> <body> <h2>Book List</h2> <table border="1"> <tr> <th>Title</th> <th>Author</th> <th>Category</th> </tr> { for $b in /library/book return <tr> <td>{ $b/title/text() }</td> <td>{ $b/author/text() }</td> <td>{ $b/@category }</td> </tr> } </table> </body></html>Resulting HTML
<html> <head> <title>Library Books</title> </head> <body> <h2>Book List</h2> <table border="1"> <tr> <th>Title</th> <th>Author</th> <th>Category</th> </tr> <tr> <td>The Hobbit</td> <td>J.R.R. Tolkien</td> <td>fiction</td> </tr> <tr> <td>Sapiens</td> <td>Yuval Noah Harari</td> <td>history</td> </tr> </table> </body></html>Summary
XQuery can build HTML by creating XML elements representing HTML tags.
Dynamic content inside curly braces
{ ... }is evaluated and inserted.Useful for XML data-driven web pages and reports.
Want help creating customized HTML output with XQuery?