Subject:Omiting the XML declaration from Xqueries Author:Steven Fancher Date:26 Mar 2006 02:35 PM
All,
I've been feeding some simple Xqueries to the SAXON parser from a java class. Works like a charm. However, I'd like the output to exclude the standard <?xml version="1.0" encoding="ISO-8859-1"?> declaration.
When I run the query in StylusStudio the result does not contain the declaration, but when I run it from java I get the decl. There seem to be references to an omit-xml-declaration setting but the only place I see it is in stylesheets.
I can just post-process the results in Java of course, but that seems architecturally offensive......
I also get that this is not strictly an XML problem, but a serializer problem. None the less I can't figure out how to do it, so I figured I' ask people smarter than I :-)
(BTW any other random criticizims of the code below are welcome!)
The Java class that makes the call is below:
============================================
// run an Xquery against a saxon document
public static String runQuery(net.sf.saxon.jdom.DocumentWrapper doc,
String query) {
// the result
ByteArrayOutputStream os = new ByteArrayOutputStream();
StreamResult result = new StreamResult(os);
// stores saxon internals
final Configuration config = new Configuration();
try {
// set up the query
final StaticQueryContext sqc = new StaticQueryContext(config);
final XQueryExpression exp = sqc.compileQuery(query);
// wire in the passsed node as source
final DynamicQueryContext dynamicContext =
new DynamicQueryContext(config);
dynamicContext.setContextItem(doc);
// run the query
exp.run(dynamicContext, result, new Properties());
} catch (Exception e) {
e.printStackTrace();
}
return os.toString();
}
Or you may want to take a look at the Java code generated by Stylus Studio for XQuery/Saxon (XQuery > Generate Java Code); it looks like it never generates the XML PI.