tags:

views:

486

answers:

6

I have been reading lot of XQUERY tutorial on the web site. Almost all of them are teaching me the XQUERY syntax. Let's say i have understood the XQUERY syntax, how am i going to actually implement XQUERY on my web site?

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 example. Or any web site that i can do reference. Thanks very much.

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

I haven't tested it either, but I suspect that you want @id = $book/authorid. Not sure how eq compares in those cases - I think it compares references, not values.
Tirno
A: 

hi, asdf.

thanks for your suggestion. but i am still wondering where should i put the code that you suggested above?

am i going to save the code as a html, xml or javascipt file?

+2  A: 
(: 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>
}

frglps
A: 

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.

Mattio
+1  A: 

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.

Sixty4Bit
Relational databases like Oracle and Sql Server support XQuery too.
tuinstoel
+1  A: 

You need a server or a library to process the xml into html. In my opinion, XQuery is much better than XSTL 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.

MattMcKnight