The following JUnit test works for me:
 
public void testIterator() {
        try {
            Configuration c = new Configuration();
            String xml = "<a><b>3</b><b>5</b></a>";
            DocumentInfo doc = c.buildDocument(new StreamSource(new StringReader(xml)));
            StaticQueryContext s = new StaticQueryContext(c);
            XQueryExpression exp = s.compileQuery("//b");
            DynamicQueryContext dqc = new DynamicQueryContext(c);
            dqc.setContextItem(doc);
            SequenceIterator iter = exp.iterator(dqc);
            int count = 0;
            while (true) {
                Item item = iter.next();
                if (item == null) {
                    break;
                }
                count++;
                assertTrue((iter.position()==1 && item.getStringValue().equals("3")) ||
                            (iter.position()==2 && item.getStringValue().equals("5")));
            }
            assertTrue("count", count==2);
        } catch (XPathException e) {
            fail(e.getMessage());
        }
    }
 
Perhaps you could give it a try. If you still have problems, please supply a complete runnable example (preferably in a similar form to the above). And please confirm what Saxon release you are using (you mention Saxon 8 in the subject heading. The current release is 9.1).
 

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay



From: CJ Fleck [mailto:list.cj@gmail.com]
Sent: 12 May 2009 20:47
To: saxon-help@lists.sourceforge.net
Subject: [saxon] Saxon 8 Problem with SequenceIterator

I'm having a problem with some results from an XQueryExpression.  Basically, If I use .evaluate(DQC) to get the results it seems to work.  If I use .iterator(DQC) it only gives the first result it comes across.

My code is below.  The setup is the same for both snippets 1 and 2, and the sample xml is in snippet 3.  If I use the query of "//b", snippet 1 will give me 2 results.  That is what I expect.  If it use that same query for snippet 2, I get a single result, and it is always the first one in the xml file.  Why is this the case?  Are my expectations off?

======= snippet 1 =================
for(Object o : exp.evaluate(DQC)){
  System.out.println(o);
}
======= snippet 2 =================
SequenceIterator iter = exp.iterator(DQC);
while (true) {
  Item item = iter.next();
  if (item == null) {
    break;
  }
  System.out.println(query + " - " + iter.position() + " - " + item.getStringValue());
}
======= snippet 3 =================
<a>
  <b>3</b>
  <b>5</b>
</a>