Project

Profile

Help

recursive invocations of XQueryExpression?

Added by Anonymous over 15 years ago

Legacy ID: #7410951 Legacy Poster: newbee (nakoned)

Hi, Did anyone experienced problems with XQueryExpression while performing recursive query look ups on the document and elements? It seems like I stumbled at the issue of processor being able to process documents, but having issues with Elements of sub-tree. Here is a code snippet that shows simple query statements: package saxontest; import javax.xml.transform.stream.StreamSource; import java.io.; import java.util.; import net.sf.saxon.query.StaticQueryContext; import net.sf.saxon.query.XQueryExpression; import net.sf.saxon.query.DynamicQueryContext; import net.sf.saxon.Configuration; import net.sf.saxon.trans.XPathException; import net.sf.saxon.om.DocumentInfo; import net.sf.saxon.om.SequenceIterator; import net.sf.saxon.om.Item; public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { Configuration config = new Configuration(); StaticQueryContext staticQueryContext = new StaticQueryContext(config); try { XQueryExpression exp1 = staticQueryContext.compileQuery("//event"); FileReader reader = new FileReader("testXml.xml"); DocumentInfo doc = staticQueryContext.buildDocument(new StreamSource(reader)); List lst = evaluateXQueryPath(staticQueryContext, exp1, doc); XQueryExpression exp2 = staticQueryContext.compileQuery("data(//name)"); Iterator it = lst.iterator(); Item item = null; while ((item = (Item)it.next()) != null) { List lst2 = evaluateXQueryPath(staticQueryContext, exp2, item); Iterator it2 = lst2.iterator(); while ((item = (Item)it2.next()) != null) { System.out.println(item.getStringValue()); } } } catch(Exception e) { e.printStackTrace(); } } public static List evaluateXQueryPath(StaticQueryContext staticQueryContext, XQueryExpression exp, Item xml) throws XPathException { Configuration config = staticQueryContext.getConfiguration(); DynamicQueryContext dynamicContext = new DynamicQueryContext(config); dynamicContext.setContextItem(xml); return createListOfXmlNodes(exp, dynamicContext); } public static List createListOfXmlNodes(XQueryExpression exp, DynamicQueryContext dynamicContext) throws XPathException { final SequenceIterator iter = exp.iterator(dynamicContext); List listVariable = new ArrayList(); while (true) { Item item = iter.next(); if (item == null) { break; } listVariable.add(item); } return listVariable; } } And this is an input xml file: <?xml version="1.0" encoding="ISO-8859-15"?> <events> <event> <number>130082837</number> <name>Steve</name> </event> <event> <number>130110661</number> <name>Bill</name> </event> </events> If I run this code I'd expect that I get 2 names printed in DIFFERENT iterations of the second while loop. However, I get both printed on the first and exception thrown in the second. Could someone help? Thanks, Ed


Replies (2)

RE: recursive invocations of XQueryExpression? - Added by Anonymous over 15 years ago

Legacy ID: #7411161 Legacy Poster: Michael Kay (mhkay)

The reason you get two names during the first iteration is because you used "//name" when you probably meant "name" or ".//name". Short of running the code myself I don't know why you got an exception the second time round; telling us what the exception was would give me a head start. Come to think of it, staring at the code a bit longer, you seem to be expecting next() on an Iterator to return null at the end of the sequence. That's how Saxon's SequenceIterator works, but it's not how a standard Java iterator works. It's not clear to me why you are reading all the results into a list and then iterating over the list, when you could just iterate over the results directly.

RE: recursive invocations of XQueryExpression? - Added by Anonymous over 15 years ago

Legacy ID: #7413217 Legacy Poster: newbee (nakoned)

thanks for the response.. I did not realize that //name will start looking from the root element. I thought that if I specify element as a context, XQuery will use it is a root. Apparently I was wrong. Setting query as ".//name" works. Thanks

    (1-2/2)

    Please register to reply