XQuery -> NodeInfo?
Added by Anonymous almost 19 years ago
Legacy ID: #3494969 Legacy Poster: bilbao6566545 (bilbao6566545)
Hi,i am doing xquery and next xsl transformation. Output of xquery is NodeInfo which i use as source for xslt. Problem is that with some result of xquery xslt works ok etc:"/" and with some not etc: "<A>{//B}</A>. It is writing me: Error Source tree must have a document node as its root Here is my source code: //first build dom and do some x-path queries String xdata="<Response><Data><A>a</A></Data></Response>"; Configuration cfg = new Configuration(); cfg.setTreeModel(Builder.TINY_TREE); XPathEvaluator xpe = new XPathEvaluator(); NodeInfo xmlDoc = xpe.setSource(new StreamSource(new StringReader(xdata))); //XQuery String xq = "<Response><Data><B></B></Data></Response>";//"/"; Configuration config = new Configuration(); StaticQueryContext sqc = new StaticQueryContext(config); XQueryExpression exp = sqc.compileQuery(xq); DynamicQueryContext dynamicContext = new DynamicQueryContext(config); dynamicContext.setContextNode(xmlDoc); Properties props = new Properties(); NodeInfo xq_doc = (NodeInfo) exp.evaluateSingle(dynamicContext); //XSLT File xslfile = new File("transf.xsl"); StreamSource xsl_source = new StreamSource(xslfile); System.setProperty("javax.xml.transform.TransformerFactory","net.sf.saxon.TransformerFactoryImpl"); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(xsl_source); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); transformer.transform(xq_doc,result); //place where this error occurs String xslres = writer.toString(); What can be wrong? I try to use NodeInfo, and passing it to other tranformation, is it the quickest method,isn't it? Thank you, Bilba
Replies (3)
Please register to reply
RE: XQuery -> NodeInfo? - Added by Anonymous almost 19 years ago
Legacy ID: #3495362 Legacy Poster: Michael Kay (mhkay)
As the message implies, Saxon currently requires the principal source tree for an XSLT transformation to have a document node at its root. The reasons for this are partly historical, and partly because there have been areas of the spec that were not clearly defined if this was not the case: see in particular http://www.w3.org/Bugs/Public/show_bug.cgi?id=2467. I think these issues are now resolved, so I'll consider lifting the restriction. In the meantime, simply change your query to add a document node: document-node{ <a>{//B}</a> } I'd actually be inclined to do this anyway, since handling parentless elements in XSLT can be quite confusing, for example any path expression starting with "/" throws an error. Michael Kay
RE: XQuery -> NodeInfo? - Added by Anonymous almost 19 years ago
Legacy ID: #3496162 Legacy Poster: bilbao6566545 (bilbao6566545)
Thank you again, that works. I also tried construction like that: TinyBuilder xqdoc_build = new TinyBuilder(); exp.run(dynamicContext, xqdoc_build, new Properties()); xqdoc_build.getCurrentRoot(); and with that it works also, but it is slower.
RE: XQuery -> NodeInfo? - Added by Anonymous almost 19 years ago
Legacy ID: #3500518 Legacy Poster: Michael Kay (mhkay)
In my previous response, please note that the XQuery constructor for a document node is document{<a/>} rather than document-node{<a/>} as suggested. I have now enhanced the XSLT engine so that the initial context node is not required to be in a tree rooted at a document node; the rules for evaluating global variables are as described in the resolution of issue 2467. I've also discovered that Saxon in XQuery applies the same rule as in XSLT that in global variables, the context item is the root of the tree containing the supplied context item. There's no such rule in XQuery (though the actual rules seem a little unclear). MK
Please register to reply