Project

Profile

Help

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

he / latest9.3 / hej / net / sf / saxon / query / package.html @ 452a06ef

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 a relatively low-level API for executing XQuery queries
10
directly from a Java application. This is no longer the recommended API for writing Java
11
    applications using XQuery: there are two higher-level APIs, s9api ({@link net.sf.saxon.s9api},
12
    and XQJ ({@link javax.xml.xquery}. This package also includes internal supporting code
13
that implements XQuery within Saxon.</p>
14

    
15
<p>For details of the API, see the JavaDoc documentation of individual classes, starting
16
with the {@link net.sf.saxon.query.StaticQueryContext}.</p>
17

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

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

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

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

    
59
<p>To execute your compiled query, you need to create a {@link net.sf.saxon.query.DynamicQueryContext} object
60
that holds the run-time context information. The main things you can set in the run-time context are:</p>
61

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

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

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

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

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

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

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

    
118

    
119
<p align="center"><i>Michael H. Kay<br/>
120
Saxonica Limited<br/>
121
30 July 2010</i></p>
122
</body>
123
</html>
(19-19/19)