Listing 1

<?xml version="1.0" ?>
- <books name="My books">
- <book bookid="1" pubdate="03/01/2002">
 <title>Java Web Services</title>
- <authors>
 <author>David A. Chappel</author>
 <author>Tyler Jewell</author>
 </authors>
 <subject>Web Services</subject> 
 </book>
- <book bookid="2" pubdate="01/01/2000">
 <title>Java Message Service</title> 
- <authors>
 <author>David A. Chappel</author> 
 </authors>
 <subject>Java Message Service (JMS)</subject> 
 </book>
- <book bookid="3" pubdate="03/01/2002">
 <title>Professional ebXML Foundations</title> 
- <authors>
 <author>Colleen Evans</author> 
 <author>David A. Chappel</author> 
 <author>Jean-Jacques Dubray</author> 
 <author>Duane Nickull</author> 
 <author>Pim van der Eijk</author> 
 <author>Vivek Chopra</author> 
 <author>Betty Harvey</author> 
 <author>Marcel Noordzij</author> 
 <author>Jan Vegt</author> 
 <author>Tim McGrath</author> 
 <author>Bruce Peat</author> 
 </authors>
 <subject>ebXML</subject> 
 </book>
- <book bookid="4" pubdate="01/03/1997">
 <title>Beginning Visual C++ 6 Database Programming</title> 
- <authors>
 <author>John Connell</author> 
 <author>Minollo</author> 
 </authors>
 <subject>Database</subject> 
 </book>
- <book bookid="5" pubdate="11/10/2000">
 <title>Beginner's Guide to Access 2.0</title> 
- <authors>
 <author>Wrox Author Team</author> 
 <author>minollo@minollo.com</author> 
 </authors>
 <subject class="1">Access</subject> 
 </book>
- <book bookid="6" pubdate="11/12/1999">
 <title>Beginning Java 2</title> 
- <authors>
 <author>Ivor Horton</author> 
 </authors>
 <subject>Java</subject> 
 </book>
- <book bookid="7" pubdate="02/07/1998">
 <title>Beginning ATL COM Programming</title> 
- <authors>
 <author>Richard Grimes</author> 
 <author>George Reilly</author> 
 <author>Alex Stockton</author> 
 <author>Julian Templeman</author> 
 </authors>
 <subject>C++</subject> 
 </book>
- <book bookid="8" pubdate="10/01/1999">
 <title>XML Applications</title> 
- <authors>
 <author>Frank Boumphrey</author> 
 <author>Olivia di Renzo</author> 
 <author>Jon Duckett</author> 
 <author>Joe Graf</author> 
 <author>Dave Hollander</author> 
 <author>Paul Houle</author> 
 <author>Trevor Jenkins</author> 
 <author>Peter Jones</author> 
 <author>Adrian Kingsley-Hughes</author> 
 <author>Kathie Kingsley-Hughes</author> 
 <author>Craig McQueen</author> 
 <author>Stephen Mohr</author> 
 </authors>
 <subject>XML</subject> 
 </book>
- <book bookid="9" pubdate="11/12/1999">
 <title>Instant UNIX</title> 
- <authors>
 <author>Andrew Evans</author> 
 <author>Neil Matthew</author> 
 <author>Richard Stones</author> 
 </authors>
 <subject>GNU/Linux</subject> 
 </book>
 </books>


Listing 2

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="books">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="book"/>
      </xs:sequence>
      <xs:attribute name="name" use="required"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="book">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="title"/>
        <xs:element ref="authors"/>
        <xs:element ref="subject"/>
      </xs:sequence>
      <xs:attribute name="bookid" use="required" type="xs:integer"/>
      <xs:attribute name="pubdate" use="required"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="title" type="xs:string"/>
  <xs:element name="authors">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="author"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="author" type="xs:string"/>
  <xs:element name="subject">
    <xs:complexType mixed="true">
      <xs:attribute name="class" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
</xs:schema>


Listing 3

package com.vsp;

import com.ddtek.xmlconverter.ConverterFactory;
import com.ddtek.xmlconverter.ConverterResolver;
import com.saxonica.validate.SchemaAwareConfiguration;
import java.io.File;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.URI;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.Configuration;
import net.sf.saxon.om.Item;
import net.sf.saxon.om.SequenceIterator;
import net.sf.saxon.om.Validation;
import net.sf.saxon.query.DynamicQueryContext;
import net.sf.saxon.query.StaticQueryContext;
import net.sf.saxon.query.XQueryExpression;
import net.sf.saxon.value.AtomicValue;
import net.sf.saxon.value.SequenceExtent;
import net.sf.saxon.value.UntypedAtomicValue;
import net.sf.saxon.value.Value;
import net.sf.saxon.value.Whitespace;

/** Run an XQuery
 * This class requires the following items in the classpath.
 * C:/Program Files/StylusStudio
 * C:/Program Files/Stylus Studio 2007 XML Enterprise Suite Release 2/bin/XMLConverters.jar
 * C:/Program Files/Stylus Studio 2007 XML Enterprise Suite Release 2/bin/saxon8sa.jar
 */

public class Author {

 public static void main(String[] args) throws Exception {

  ConverterResolver resolver = new ConverterFactory().newResolver();

  String    inputUrl     = "file:///c:/books.xml";
  String    xqueryUrl    = "file:///c:/author.xquery";
  Reader    queryReader  = null;


  try {

   System.out.println();
   System.out.println("XQuery starting.");

   queryReader = new FileReader(new File(new URI(xqueryUrl)));

   Configuration config = getConfiguration(resolver);
   StaticQueryContext staticContext = new StaticQueryContext(config);
   staticContext.setBaseURI(xqueryUrl);

   DynamicQueryContext dynamicContext  = new DynamicQueryContext(config);
   dynamicContext.setContextItem(staticContext.buildDocument(new StreamSource(inputUrl)));

   XQueryExpression expr = staticContext.compileQuery(queryReader);
   expr.run(dynamicContext, new StreamResult(new OutputStreamWriter(System.out)), null);

   System.out.println("XQuery finished.");
  } finally {
   if (queryReader != null) queryReader.close();
  }
 }

 private static Configuration getConfiguration(ConverterResolver resolver) {

  SchemaAwareConfiguration config = new SchemaAwareConfiguration();
  config.getSystemURIResolver().setRecognizeQueryParameters(true);
  config.setValidation(false);
  config.setStripsWhiteSpace(Whitespace.IGNORABLE);
  config.setSchemaValidationMode(Validation.LAX);
  config.setValidationWarnings(true);
  config.setURIResolver(resolver);
  return config;
 }


 private static void bindParameter(StaticQueryContext staticContext, DynamicQueryContext dynamicContext,
    String paramName, String paramValue)
 {
  Value xvalue=null;
  try {
   XQueryExpression xqe = staticContext.compileQuery(paramValue);
   SequenceIterator si = xqe.iterator(dynamicContext).getAnother();
   Item item = si.next();
   if (item != null && si.next() == null && item instanceof AtomicValue) {
    xvalue = (AtomicValue)item;
   } else {
    xvalue = new SequenceExtent(xqe.iterator(dynamicContext));
   }
  } catch (Throwable t) {
   xvalue = new UntypedAtomicValue(paramValue);
  }

  dynamicContext.setParameter(paramName, xvalue);
 }
}