The JAXP specs don't make this clear (something I've complained about for years), but some kinds of Source are consumed when they are used: this includes a StreamSource and a SAXSource. Once you've supplied such a source to a method such as transform(), you can't use it again. In this respect it's like an InputStream or a Reader. If you want to use the XML document more than once, you have to build a tree representation of the document in memory, for example by using the buildDocument() method provided on StaticQueryContext.
Michael Kay

From: [] On Behalf Of Mohana Ramaratnam
Sent: 11 October 2006 22:14
Subject: [saxon] SXXP0003 Premature end of file



I am trying to resolve many xpath statements against a document. I have read the document into a SAXSource and am calling the method resolveXPath as below:


public ArrayList resolveXPath(String xpathStmt, SAXSource ss,XMLWrapperElement element ) throws Exception {

        ArrayList rtn = new ArrayList();


        XPathEvaluator xpe = new XPathEvaluator();

        if (element.getSchemaTargetNamespacePrefix() != null && element.getSchemaTargetNamespacePrefix().equals("") && element.getSchemaTargetNamespaceURI() != null) {

            ((IndependentContext)xpe.getStaticContext()).declareNamespace("", element.getSchemaTargetNamespaceURI());

        }else if (element.getSchemaTargetNamespacePrefix() != null && !element.getSchemaTargetNamespacePrefix().equals("") && element.getSchemaTargetNamespaceURI() != null) {

            ((IndependentContext)xpe.getStaticContext()).declareNamespace(element.getSchemaTargetNamespacePrefix(), element.getSchemaTargetNamespaceURI());


        XPathExpression xExpr =  xpe.createExpression(xpathStmt);

        SequenceIterator rtns = xExpr.rawIterator(ss);

        Item xpathObj =;

        while (xpathObj != null) {

            xpathObj = rtns.current();


            xpathObj =;


        return rtn;



I get the following error. However, if I read the document everytime for an xpath statement, I don’t get the error, leading me to believe that the XPathExpression.rawIterator is corrupting the SAXSource. Am I making any mistake? How should I fix this?





  SXXP0003: Error reported by XML parser: Premature end of file.

net.sf.saxon.trans.DynamicError: org.xml.sax.SAXParseException: Premature end of


        at net.sf.saxon.event.Sender.sendSAXSource(

        at net.sf.saxon.event.Sender.send(

        at net.sf.saxon.event.Sender.send(




        at net.sf.saxon.sxpath.XPathExpression.rawIterator(