Project

Profile

Help

How to connect?
Download (8.32 KB) Statistics
| Branch: | Tag: | Revision:

he / tags / 9.2.0.5 / hej / net / sf / saxon / query / package.html @ 90ee0d1a

1
<html>
2

    
3
<head>
4
<title>Package overview for net.sf.saxon.query</title>
5
</head>
6

    
7
<body>
8

    
9
<p>This package provides an API for executing XQuery queries
10
directly from a Java application. It also includes the internal supporting code
11
that implements XQuery within Saxon.</p>
12

    
13
<p>For details of the API, see the JavaDoc documentation of individual classes, starting
14
with the <code>StaticQueryContext</code>.</p>
15

    
16
<p>The first thing you need to do is to create a <code>net.sf.saxon.Configuration</code> object.
17
This holds values of all the system settings, corresponding to flags available on the command line.
18
You don't need to set any properties in the <code>Configuration</code> object if you are happy
19
with the default settings. For schema-aware processing, you will need to create a
20
<code>com.saxonica.config.EnterpriseConfiguration</code> instead.</p>
21

    
22
<p>Then you need to create a <code>StaticQueryContext</code> object. As the name
23
implies, this holds information about the static (compile-time) context for a query. Most aspects
24
of the static context can be defined in the Query Prolog, but this object allows you to initialize
25
the static context from the application instead if you need to. Some of the facilities provided are
26
very much for advanced users only, for example the ability to declare variables and functions, and
27
the ability to specify a NamePool to be used. One aspect of the static context that you may need
28
to use is the ability to declare collations. Using the method <code>declareCollation</code> you can
29
create a mapping between a collation URI (which can then be used anywhere in the Query) and a Java
30
<code>StringCollator</code> object used to implement that collation.</p>
31

    
32
<p>Having created, and possibly configured, the <code>Configuration</code> and
33
<code>StaticQueryContext</code> objects, you can now compile a Query using the <code>compileQuery</code>
34
method on the <code>StaticQueryContext</code>. The text of the
35
Query can be supplied either as a <code>String</code> or as a Java <code>Reader</code>. There
36
are thus two different <code>compileQuery</code> methods. Each of them returns the compiled
37
query in the form of a <code>XQueryExpression</code>. The <code>XQueryExpression</code>, as you would expect,
38
can be executed repeatedly, as often as you want, in the same or in different threads.</p>
39

    
40
<p>Before you run your query, you may want to build one or more trees representing
41
XML documents that can be used as input to your query. You don't need to do this: if the query
42
loads its source documents using the <code>doc()</code> function then this will be done
43
automatically, but doing it yourself gives you more control. A document node at the root of
44
a tree is represented in Saxon by the <code>net.sf.saxon.DocumentInfo</code> interface.
45
The <code>StaticQueryContext</code> provides a convenience method, <code>buildDocument()</code>,
46
that allows an instance of <code>DocumentInfo</code> to be constructed. The input parameter to
47
this is defined by the class <code>javax.xml.transform.Source</code>, which is part of the 
48
standard Java JAXP API: the <code>Source</code> interface is an umbrella for different kinds of
49
XML document source, including a <code>StreamSource</code> which parses raw XML from a byte
50
or character stream, <code>SAXSource</code> which takes the input from a SAX parser (or an
51
object that is simulating a SAX parser), and <code>DOMSource</code> which provides the input
52
from a DOM. Saxon also provides a <code>net.sf.saxon.jdom.DocumentWrapper</code> which allows
53
the input to be taken from a JDOM document.</p>
54

    
55
<p>To execute your compiled query, you need to create a <code>DynamicQueryContext</code> object
56
that holds the run-time context information. The main things you can set in the run-time context are:</p>
57

    
58
<ul>
59
<li>Values of parameters (external global variables). You can set these using the <code>setParameter()</code>
60
method. The mappings from Java classes to XQuery/XPath data types is the same as the mapping used for the
61
returned values from an external Java method call, and is described under 
62
<a href="extensibility.html#function-result">Result of an Extension Function</a>.</li>
63
<li>The context node can be set using the method <code>setContextNode()</code>. For some reason
64
it isn't possible to set a context item other than a node.</li>
65
<li>You can also set a URIResolver and/or ErrorListener. These default to the ones that were
66
used during Query compilation.</li>
67
</ul>
68

    
69
<p>You are now ready to evaluate the query. There are several methods on the <code>QueryExpression</code>
70
object that you can use to achieve this. The <code>evaluate()</code> method returns the result sequence 
71
as a Java <code>java.util.List</code>. The <code>evaluateSingle()</code> method is suitable when you know
72
that the result sequence will contain a single item: this returns this item as an Object, or returns null
73
if the result is an empty sequence. There is also an <code>iterator</code> method that returns an iterator
74
over the results. This is a Saxon object of class <code>net.sf.saxon.SequenceIterator</code>: it is similar
75
to the standard Java iterator, but not quite identical; for example, it can throw exceptions.</p>
76

    
77
<p>The <code>evaluate()</code> and <code>evaluateSingle()</code> methods return the result as a Java object
78
of the most appropriate type: for example a String is returned as a <code>java.lang.String</code>, a 
79
boolean as a <code>java.lang.Boolean</code>. A node is returned using the Saxon representation of a node,
80
<code>net.sf.saxon.om.NodeInfo</code>. With the standard and tinytree models, this object also implements
81
the DOM <code>Node</code> interface (but any attempt to update the node throws an error).</p>
82

    
83
<p>The <code>iterator()</code> method, by contrast, does not do any conversion of the result. It is returned
84
using its native Saxon representation, for example a String is returned as an instance of 
85
<code>sf.net.saxon.value.StringValue</code>. You can then use all the methods available on this class
86
to process the returned value.</p>
87

    
88
<p>If you want to process the results of the query in your application, that's all there is to it. But you
89
may want to output the results as serialized XML. Saxon provides two ways of doing this: you can produce
90
wrapped output, or raw output. Raw output works only if the result consists of a single document or element
91
node, and it outputs the subtree rooted at that element node in the form of a serialized XML document. Wrapped
92
output works for any result sequence, for example a sequence of integers or a sequence of attribute and
93
comment nodes; this works by wrapping each item in the result sequence as an XML element, with details
94
of its type and value.</p>
95

    
96
<p>To produce wrapped output, you first wrap the result sequence as an XML tree, and then serialize the
97
tree. To produce unwrapped output, you skip the wrapping stage and just call the serializer directly.</p>
98

    
99
<p>Both steps can be done using the <code>QueryResult</code> class. This class doesn't need to be
100
instantiated, its methods are static. The method <code>QueryResult.wrap</code> takes as input the iterator
101
produced by evaluating the query using the <code>iterator()</code> method, and produces as output
102
a <code>DocumentInfo</code> object representing the results wrapped as an XML tree. The method
103
<code>QueryResult.serialize</code> takes any document or element node as input, and writes it to
104
a specified destination, using specified output properties. The destination is supplied as an object
105
of class <code>javax.xml.transform.Result</code>. Like the <code>Source</code>, this is part of the
106
JAXP API, and allows the destination to be specified as a StreamResult (representing a byte stream or
107
character stream), a SAXResult (which wraps a SAX ContentHandler), or a DOMResult
108
 (which delivers the result as a DOM). The output properties are used only when writing to
109
 a StreamResult: they correspond to the properties available in the <code>xsl:output</code> element
110
 for XSLT. The property names are defined by constants in the JAXP <code>javax.xml.transform.OutputKeys</code>
111
 class (or <code>net.sf.saxon.event.SaxonOutputKeys</code> for Saxon extensions): for details of the
112
 values that are accepted, see the JavaDoc documentation or the JAXP specification.</p>
113

    
114

    
115
<p align="center"><i>Michael H. Kay<br/>
116
Saxonica Limited<br/>
117
9 February 2005</i></p>
118
</body>
119
</html>
(21-21/21)