Generating XML Output with XQuery

So far all the queries we have written have selected nodes in the source document. We have shown the results as if the system copies the nodes to create some kind of result document, and if you execute the XQuery in Stylus Studio or run Saxon from the command line that is exactly what happens. But this is 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 is 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>
               

            

We have also changed the query so that the actor's first name is now an externally defined parameter. This makes the query reusable. The way parameters are supplied varies from one XQuery processor to another. In Stylus Studio, select XQuery > Scenario Properties; click the Parameter Values tab, and Stylus Studio provides an area to specify values for any variables defined in the XQuery.

Figure 293. Entering Values for XQuery Variables

Enter "Lisa", in quotes (Stylus Studio expects an expression, so if the quotes are omitted, this value would be taken as a reference to an element named <Lisa>).

If instead you are running Saxon from the command line, you can enter:


              java net.sf.saxon.Query sample.xquery firstName=Lisa
               

            

Either way, our XQuery returns the following result:

<videos featuring="Lisa">
               
   <video year="1999">
               
      <title>Enemy of the State</title>
               
   </video>
               
   <video year="1999">
               
      <title>Clerks</title>
               
   </video>
               
</videos> 
               

            

As you might recall from our previous XQuery, this version of the XQuery is not especially well-designed as it returns videos featuring different actresses named Lisa. Take some time and modify the XQuery to see if you can improve it.

Whitepaper: FIXML - Working with Complex Message Sets Defined Using XML Schema

The advanced XML Schema handling and presentation capabilities of Stylus Studio have valuable implications for users of industry-specific XML message sets.

Data Integration Tools: Using the Stylus Studio XML Pipeline to Visually Integrate and Publish Data

Data Integration Tools from Stylus Studio include a visual pipeline editor, support for non-XML data sources, scalable data integration components, cross-language debugging and Java code generation. Try it now.

XML Processing

Learn how to simplify XML Processing using Stylus Studio XML Pipeline, the easiest way to visually design and deploy virtually any XML processing tasks.

W3C XML Specifications

Learn about the official W3C XML specifications for XML, XSLT, XQuery, XPath, DOM, XML Namespaces, XInclude, XML Base, XPointer, WSDL and more.

Stylus Most Wanted

 
Free Stylus Studio XML Training:
W3C Member