XQuery with non XML source
Added by Anonymous about 16 years ago
Legacy ID: #5256485 Legacy Poster: filipix (filipix)
Hello, I am trying to use Saxon with non XML data and to test that I am using a filesystem as a source with the pseudo XML representation of the filesystem is : <Directory size="4096" fileName="src" filePath="/home/filipix/devt/TestXmlPatterns/src" mimeType="application/octet-stream" suffix=""> <Directory size="4096" fileName="com" filePath="/home/filipix/devt/TestXmlPatterns/src/com" mimeType="application/octet-stream" suffix=""> <File size="10745" fileName="frank.xbel" filePath="/home/filipix/devt/TestXmlPatterns/src/com/frank.xbel" mimeType="application/octet-stream" suffix="xbel"/> ..... I wrote an implementation for NodeInfo and DocumentInfo in the same way as they are done with dom4j or xom. It is working quite well for evaluating XPath expression. By that I mean that my XML representation of the filesystem is not expanded in all directions : when evaluating the expression "Directory[@fileName="src"]//File" it only evaluates the src directory to find the files, it doesn't go through all the sibling directories of "src". When I am trying to use a XQuery to the same source, the source is loaded in memory before evaluating the query which is not what I want. I tried several things, here is one : Processor processor = new Processor(false); XQueryCompiler xqCompiler = processor.newXQueryCompiler(); // DocumentWrapper is my implementation of DocumentInfo, FileNode is the underlying adaptor DocumentWrapper dw = new DocumentWrapper(new FileNode(null, "/home/filipix/devt/TestXmlPatterns", 0), "", processor.getUnderlyingConfiguration()); // Here is a simple XQuery, just a xpath expression XQueryExecutable xqExe = xqCompiler.compile("/Directory[@fileName = "src"]//File[@suffix = "java"]"); XQueryEvaluator xqEval = xqExe.load(); // Here is where I am not happy, This expands the whole filesystem even if it's not needed xqEval.setSource(dw); // The result is ok, but as I wrote above expanding the whole XML representation before running the query is not what I want xqEval.run(out); Is there a way to run an xquery without expanding the document before? Thanks and sorry for the long post Philippe
Replies (7)
Please register to reply
RE: XQuery with non XML source - Added by Anonymous about 16 years ago
Legacy ID: #5256593 Legacy Poster: Michael Kay (mhkay)
I suspect that the s9api method setSource() is always making a copy of the supplied source - surprisingly, I was looking into a very similar problem on the XSLT side yesterday. So try setContextItem() instead. You will have to wrap the DocumentWrapper in an XdmNode first. Embarrassingly, I can't see any way of doing this at the moment other than creating an XdmValue from a single-item List containing your node, and then using itemAt(0) to get its first item. Michael Kay
RE: XQuery with non XML source - Added by Anonymous about 16 years ago
Legacy ID: #5257652 Legacy Poster: filipix (filipix)
Thanks Michael for the answer, I changed my code as you suggested (without the trouble of the List) : XdmNode node = processor.newDocumentBuilder().wrap(dw); xqEval.setContextItem(node); but when I do xqEval.run() there is an exception : IllegalStateException("No destination supplied"); and when I do xqEval.run(out) or evaluate I have the error message below. What can of Destination may I set ? Thanks Philippe Error on line 0 of module with no systemId: XQTY0024: An attribute node (size) cannot be created after the children of the containing element net.sf.saxon.s9api.SaxonApiException: An attribute node (size) cannot be created after the children of the containing element at net.sf.saxon.s9api.XQueryEvaluator.evaluate(XQueryEvaluator.java:293) at com.cadesis.spike.test.TestXQuery.main(TestXQuery.java:66) Caused by: net.sf.saxon.event.NoOpenStartTagException: An attribute node (size) cannot be created after the children of the containing element at net.sf.saxon.event.NoOpenStartTagException.makeNoOpenStartTagException(NoOpenStartTagException.java:49) at net.sf.saxon.event.ComplexContentOutputter.attribute(ComplexContentOutputter.java:296) at net.sf.saxon.om.Navigator.copy(Navigator.java:580) at com.cadesis.spike.NodeWrapper.copy(NodeWrapper.java:142) at net.sf.saxon.om.Navigator.copy(Navigator.java:570) at com.cadesis.spike.NodeWrapper.copy(NodeWrapper.java:142) at net.sf.saxon.event.SequenceWriter.append(SequenceWriter.java:321) at net.sf.saxon.expr.Expression.process(Expression.java:476) at net.sf.saxon.instruct.ElementCreator.constructElement(ElementCreator.java:376) at net.sf.saxon.instruct.ElementCreator.evaluateItem(ElementCreator.java:322) at net.sf.saxon.instruct.Instruction.iterate(Instruction.java:308) at net.sf.saxon.query.XQueryExpression.iterator(XQueryExpression.java:307) at net.sf.saxon.s9api.XQueryEvaluator.evaluate(XQueryEvaluator.java:281) ... 1 more
RE: XQuery with non XML source - Added by Anonymous about 16 years ago
Legacy ID: #5257802 Legacy Poster: David Lee (daldei)
Whats your xquery look like ? That last message is one you get when you try to output an attribute thats not part of an element. e.g. the query //@attribute cant be serialized. you need to do simething like //@attribute/string()
RE: XQuery with non XML source - Added by Anonymous about 16 years ago
Legacy ID: #5258013 Legacy Poster: filipix (filipix)
My xquery is : /Directory[@fileName = "src"]//File This query works fine when I use my first method : setSource(dw). I have the NoOpenStartTagException when I use setContextItem(node) and it tries to output some result to the destination. Since I expand my nodes only when asked, Navigator.copy() doesn't like that...
RE: XQuery with non XML source - Added by Anonymous about 16 years ago
Legacy ID: #5258087 Legacy Poster: Michael Kay (mhkay)
Writing your own implementation of the NodeInfo interface gives you vast opportunities to get things wrong. I'm hesitant to say I can't support you in this, but I do have to say that the onus is on you to prove that your implementation of this interface is doing the right thing. For example, that your iterator over the child axis never returns an attribute node - Saxon isn't going to check for that, but all sorts of things could go wrong if it happens. When you use setSource(), Saxon is making a copy of your input, and all path expressions are operating on that copy. This suggests you have implemented the copy() method correctly. But when you use setContextItem, Saxon doesn't copy the structure; path expressions will therefore invoke your implementation of iterateAxis() and will expose any bugs in it. I'm afraid you're going to have to do more of the debugging yourself.
RE: XQuery with non XML source - Added by Anonymous about 16 years ago
Legacy ID: #5258193 Legacy Poster: filipix (filipix)
I am very sorry about submitting the last problem and you are right : I made a mistake in my adaptor for NodeInfo, I returned the attributes nodes as a part of the children of the node.... It seems to be working as I want now : I can query my file system with XQuery and the whole filesystem is not expanded hurrah ! Well now I have to do the actual work now and connecting that to my PLM system. Thank you very much for your help.
RE: XQuery with non XML source - Added by Anonymous about 16 years ago
Legacy ID: #5258449 Legacy Poster: Michael Kay (mhkay)
No problem. I'm amazed that my wild guess turned out to be right!
Please register to reply