I'm afraid I've already demonstrated in my previous response that I'm not really an expert in XQJ - I never use it myself, as I find s9api much more intuitive. And this isn't really a Saxon question, so this might not be the best place to ask.

One of the very unfortunate design decisions in XQJ is to combine the concept of a sequence and an iterator into a single object. I thought people had known since the 1970s that this was a poor design pattern, but the XQJ designers decided to go with it apparently because that's what JDBC does. You are seeing the consequences of this decision.

An XQResultSequence can only be read in a forwards direction. It's a crazy restriction in the API, and Saxon goes to immense trouble to enforce it, despite the fact that it causes nothing but inconvenience for users. This means that once you have called getSequenceAsString(), you cannot use the result sequence again. I'm not sure why you're getting exactly the error message you are, but I think this is the problem. To investigate further I would need code that I can actually execute to reproduce the problem.

Michael Kay

On 04/03/2011 07:42, Kunal Chauhan wrote:
First of all Thanks for your replay.
As per your suggestion to bind multiple documents, I tried it and it's works for me.
But now I stuck at one point. I bind a document and get result from it and now I want to use this result for next query.
I means I want to pass this result as an input for next query. I tried below code but it gives me an error.

FileInputStream reader =


XQItem itemDoc = con.createItemFromDocument(reader, null, null);


XQPreparedExpression exp = con.prepareExpression("declare variable $lab external;<result>{$lab}</result>");


new QName("lab") , itemDoc);

XQResultSequence result = exp.executeQuery();


out.println("result=\n" + result.getSequenceAsString(null));

/*   I got whole document as result in result tag

   Now I want to process on this result */


XQPreparedExpression copy = con.prepareExpression(


XQItem xqi= result.getItem();



XQSequence result2 = copy.executeQuery();


out.println("Result = \n" + result2.getSequenceAsString(null));


/* But I am getting below error

The XQSequence is positioned before the first item

Can you suggest me how can I process on this result set */

And one more thing I want to load multiple documents once and wants to use those documents on different queries no. of times.

So how can I do it in efficient way ?

can you suggest me any way ?


Kunal Chauhan




On Thu, Mar 3, 2011 at 2:33 AM, Hans-Juergen Rennau <hrennau@yahoo.de> wrote:
Hi Kunal,

this question were better asked on a general XQuery list (e.g.
talk@x-query.com), as it is not Saxon specific. It is not Saxon specific as you
are using a standard API - the XQJ - rather than Saxon's S9API. But here come
some suggestions.

The context item can only be a single item - for example a single XML document,
or a single string. In order to let a query process more than one input item,
you can use external variables, declared like this:
declare variable $foo external;

A variable can be bound to a sequence of items. How to provide this sequence
depends on the API you use. You use, as the snippet showed, the XQJ API.
There, you have to provide an XQSequence object...

In the simplest case the object is the result of another query - than you do not
have to construct it yourself. Otherwise, you use this method offered by the
XQConnection object:
   createSequence(java.util.Iterator i)

Here comes a full example:

SaxonXQDataSource ds = new SaxonXQDataSource();
XQConnection co = ds.getConnection();
// procure nodes ...
Document d1 = ...;
Document d2 = ...;
// build an XQSequence object
List<Node> docList = new ArrayList<Node>();
XQSequence docs = co.createSequence(docList.iterator());
// bind XQSequence object to an external variable
XQExpression ex = co.createExpression();
ex.bindSequence(new QName("", "docs"), docs);
// execute query
XQSequence result = ex.executeQuery(
"declare variable $docs external; <result>{$docs}</result>");
System.out.println("result=\n" + result.getSequenceAsString(null));
In the common case that the documents are read from files and you do not want to
build DOM objects yourself, it would be a little more complicated. This would
FileInputStream is1 = new FileInputStream("foo.xml");
FileInputStream is2 = new FileInputStream("bar.xml");
XQItem i1 = co.createItemFromDocument(is1, null, null);
XQItem i2 = co.createItemFromDocument(is2, null, null);
List<XQItem> itemList = new ArrayList<XQItem>();
XQSequence items = co.createSequence(itemList.iterator());
But an alternative would be to provide only the file URIs to the query and let
the query itself retrieve the documents. This would then be like so:
 List<String> fileNameList = new ArrayList<String>();
XQSequence fileNames = co.createSequence(fileNameList.iterator());
With kind regards,
>>> You wrote:
From: Kunal Chauhan <mail4ck@gmail.com>
Subject: [saxon] saxon-help
To: saxon-help@lists.sourceforge.net

Can any one suggest me that how can we bind multiple xml file as
input source to SaxonXQExpression

for ex:
xqe = (SaxonXQExpression)con.createExpression();
xqe.bindItem(XQConstants.CONTEXT_ITEM, itemDoc);
xqe.bindItem(XQConstants.CONTEXT_ITEM, itemDocLab);

as above example i want to bind 2 document itemDoc and itemDocLab to a
single SaxonXQExpression object

Free Software Download: Index, Search & Analyze Logs and other IT data in
Real-Time with Splunk. Collect, index and harness all the fast moving IT data
generated by your applications, servers and devices whether physical, virtual
or in the cloud. Deliver compliance at lower cost and gain new business
insights. http://p.sf.net/sfu/splunk-dev2dev
saxon-help mailing list archived at http://saxon.markmail.org/

------------------------------------------------------------------------------ What You Don't Know About Data Connectivity CAN Hurt You This paper provides an overview of data connectivity, details its effect on application quality, and explores various alternative solutions. http://p.sf.net/sfu/progress-d2d
_______________________________________________ saxon-help mailing list archived at http://saxon.markmail.org/ saxon-help@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/saxon-help