Free since 2005 · No login required
AT

Academic Tutorials

Learn at your own pace

site-mobile-top-banner · 320x50

Generating XML Output with XQuery

Added 26 Jul 2008

So far all the queries we've written have selected nodes in the source document. I've shown the results as if the system copies the nodes to create some kind of result document, and if you run Saxon from the command line that's exactly what happens; but that's simply a default mode of execution. In a real application you want control over the form of the output document, which might well be the input to another application — perhaps the input to an XSLT transformation or even another query.

XQuery allows the structure of the result document to be defined using an XML-like notation. Here's an example that fleshes out our previous query with some XML markup:

declare variable $firstName as xs:string external;
<
videos featuring="{$firstName}">
{
   
let $doc := .
   
for $v in $doc//video,
      $a
in $doc//actors/actor
   
where ends-with($a, $firstName)
      
and $v/actorRef = $a/@id
   
order by $v/year
   
return
      <
video year="{$v/year}">
         {$v/title}
      video>
}
videos>