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
http://www.saxonica.com/


From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of Mohana Ramaratnam
Sent: 11 October 2006 22:14
To: saxon-help@lists.sourceforge.net
Subject: [saxon] SXXP0003 Premature end of file

Hello,

 

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 = rtns.next();

        while (xpathObj != null) {

            xpathObj = rtns.current();

            rtn.add(xpathObj.getStringValue());

            xpathObj = rtns.next();

        }

        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?

 

 

 

Error

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

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

 file.

        at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:278)

        at net.sf.saxon.event.Sender.send(Sender.java:124)

        at net.sf.saxon.event.Sender.send(Sender.java:46)

        at net.sf.saxon.event.Builder.build(Builder.java:209)

        at net.sf.saxon.event.Builder.build(Builder.java:161)

        at net.sf.saxon.sxpath.XPathEvaluator.build(XPathEvaluator.java:92)

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

135)