Project

Profile

Help

saxon java xpath exception

Added by Anonymous about 14 years ago

Legacy ID: #8332807 Legacy Poster: sunday (nonamesunday)

Hi I am trying to read XPath queries from a file and run it against a xml data file. I am doing it through Saxon Xpath processor. But i keep getting this error. Code i am using is from sample code provided with the library. Error i am getting is : Exception in thread "main" javax.xml.xpath.XPathExpressionException: Supplied node must be built using the same or a compatible Configuration at net.sf.saxon.xpath.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:283) at net.sf.saxon.xpath.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:375) at edu.xml.XPathExample.go(XPathExample.java:81) at edu.xml.XPathExample.main(XPathExample.java:42) - public void go(String filename) throws Exception { XPathExpression findLine = null; String readLine = null; Object document = new XPathEvaluator().setSource(new StreamSource(new File(dataPath))); System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_SAXON, "net.sf.saxon.xpath.XPathFactoryImpl"); XPathFactory xpf = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON); XPath xpe = xpf.newXPath(); if (filename != null) { this.queryPath = filename; } try { fileReader = new BufferedReader(new FileReader(queryPath)); } catch (FileNotFoundException ex) { System.out.println("File not found: " + ex.getMessage()); } xpe.setXPathVariableResolver(this); while (true) { try { readLine = fileReader.readLine(); } catch (IOException ex) { System.out.println("Error while reading file: " + ex.getMessage()); } if (readLine == null) { break; } findLine = xpe.compile(readLine); String resultString = findLine.evaluate(document); System.out.println(""); System.out.println("String Value => " + resultString); System.out.println(""); } }


Replies (1)

RE: saxon java xpath exception - Added by Anonymous about 14 years ago

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

Firstly, supplying an XPathEvaluator as the argument to evaluate() isn't going to work. It's a shame that this interface is so weakly typed. I would suggest passing a Saxon DocumentInfo as the argument to evaluate(), and building the document using Configuration config = ((XPathFactoryImpl)xpf).getConfiguration(); DocumentInfo document = configuration.buildDocument(new StreamSource(new File(dataPath))); Unfortunately there's not really any way of doing this without using Saxon-specific interfaces somewhere along the line (in which case using s9api might be rather cleaner).

    (1-1/1)

    Please register to reply