Poh
Poh

Reputation:

How XQuery is actually used?

I have been reading a lot of XQuery tutorials on the website. Almost all of them are teaching me XQuery syntax. Let's say I have understood the XQuery syntax, how am I going to actually implement XQuery on my website?

For example, I have book.xml:

<?xml version="1.0" encoding="iso-8859-1" ?>
<books>
<book>
   <title>Doraemon</title>
   <authorid>1</authorid>
</book>
<book>
   <title>Ultraman</title>
   <authorid>2</authorid>
</book>
</books>

Then, I have author.xml

<?xml version="1.0" encoding="iso-8859-1" ?>
<authors>
<author id="1">Mr A</author>
<author id="2">Mr B</author>
</authors>

I want to generate HTML which looks like following:

<table>
    <tr>  <td>Title</td>     <td>Author</td> </tr>
    <tr>  <td>Doraemon</td>  <td>Mr A</td>   </tr>
    <tr>  <td>Ultraman</td>  <td>Mr B</td>   </tr>
</table>

Please show me some examples. Or any website that I can do reference. Thanks very much.

Upvotes: 7

Views: 10224

Answers (7)

MattMcKnight
MattMcKnight

Reputation: 8290

You need a server or a library to process the xml into html. In my opinion, XQuery is much better than XSLT at this sort of thing when you are dealing with anything slightly complex. It is a much cleaner language as well. This website has a nice list of XQuery processors.

Upvotes: 4

Basel Shishani
Basel Shishani

Reputation: 8187

There can be many scenarios for using XQuery in a website development setting:

Generating pages dynamically:

You would need a library that provides an API that you can call from your server-side code, this would be the case if your XML data is stored say in a conventional database or on the file system. For example: Zorba provides such an API for PHP, and there is the XQuery API for Java etc.

If your XML data is stored in an XML database server that supports XQuery, then you would issue your XQuery queries to the server and get the results back. There are many open source and commercial products in this category. BaseX is an open source example.

Generating pages statically:

You might wish to generate some of the HTML pages statically from XML data. In this case you can run a command line XQuery utility, for example Zorba, Saxon, BaseX and many others provide such CLI tools. Or you can also do it from your own scripts using an API.

Then you would define rules in your build system to execute these commands or scripts whenever your XML data changes.

In both the static and dynamic approaches, you can set your environment so that XQuery plays along with your templating system, for example, instead of generating whole HTML pages by XQuery, you can generate HTML segments based on XML, and then plug them into your templates.

Uses other than transformations:

The above cases are about transforming XML to HTML, but XQuery can be used in other ways in the web development process. One way I find it useful is to modify XML documents. Say you have a long XML document and you would like to modify field values or add fields or attributes - you can use the XQuery Update Facility extension to achieve that.

Hope this helps. I didn't discuss your example because I assume it's just for clarification.

Upvotes: 1

Sixty4Bit
Sixty4Bit

Reputation: 13402

XQuery is similar to SQL in that it allows you to retrieve specific portions of data from a large data repository. SQL is used for relational databases (MS SQL Server, Oracle, Sybase, MySQL, PostreSQL, SQLite, etc...) and XQuery is used for XML databases (MARKLogic, Sedena, Qexo, Qizx/db, etc...).

MARKLogic gives you XDB servers and HTTP servers. You can have a typical web server and connect to MARKLogic through XDB or you can use their HTTP server and mix your XQuery with your HTML directly.

I suggest downloading MARKLogic's developer server (allows for 100MB of documents) and giving it a try.

Upvotes: 3

Mattio
Mattio

Reputation: 2032

To be completely honest, maybe you don't need to use XQuery at all.

If you need to transform moderately complex XML documents from XML to HTML, I would recommend using XSL. Personally, I found XSL easier to learn than XQuery. There are also a larger number of examples and tutorials available online because XSL has been around longer.

We're currently using XQuery only because it's required as part of a piece of specialized XML software we've licensed. XQuery is a fantastic tool for selecting pieces of XML from a large repository, but we still use XSL to transform our documents.

Upvotes: 2

frglps
frglps

Reputation: 721

(: file: titles.xqy :)
<table>
<tr><th>title</th><th>author</th></tr>
{
let $books-doc := doc("books.xml")
let $authors-doc := doc("authors.xml")
for $b in $books-doc//book,
    $a in $authors-doc//author
where $a/@id = $b/authorid
return 
<tr>
    <td>{$b/title/text()}</td>
    <td>{$a/text()}</td>
</tr>
}

Upvotes: 6

asdf
asdf

Reputation:

<table>
<tr><td>Title<td><td>Author<td></tr>
{
    let $authordoc := fn:doc("author.xml")
    for $book in fn:doc("book.xml")/books/book
    return
        <tr>
                <td>{ $book/title }</td>
                <td>{ $authordoc/authors/author/[@id eq $book/authorid] }</td>
        </tr>
}
</table>

ps: haven't tested/executed it, but this is how one solution could look like

Upvotes: 0

Related Questions