Using JAXP XPath API with NamespaceContext fails in 9.7 while it works in 9.6
Added by Martin Honnen over 8 years ago
The sample test case
import java.io.StringReader;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import net.sf.saxon.xpath.XPathFactoryImpl;
import org.xml.sax.InputSource;
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws XPathExpressionException {
XPathFactory xpathFactory = new XPathFactoryImpl();
XPath xpath = xpathFactory.newXPath();
xpath.setNamespaceContext(new NamespaceContext() {
@Override
public String getNamespaceURI(String prefix) {
if (prefix.equals("xhtml"))
{
return "http://www.w3.org/1999/xhtml";
}
else
{
return null;
}
}
@Override
public String getPrefix(String string) {
return null;
}
@Override
public Iterator getPrefixes(String string) {
return null;
}
});
String xhtmlSample = "<title>This is a test</title>
Test¶
";
InputSource source = new InputSource(new StringReader(xhtmlSample));
System.out.println("Found: " + xpath.evaluate("//xhtml:title", source));
}
}
compiles and runs fine against Saxon 9.6.0.8 HE while it throws a run-time exception with Saxon 9.7.0.4 HE on the evaluate call:
Exception in thread "main" java.lang.UnsupportedOperationException at net.sf.saxon.xpath.JAXPXPathStaticContext.iteratePrefixes(JAXPXPathStaticContext.java:165) at net.sf.saxon.expr.parser.RetainedStaticContext.(RetainedStaticContext.java:85) at net.sf.saxon.sxpath.AbstractStaticContext.makeRetainedStaticContext(AbstractStaticContext.java:109) at net.sf.saxon.expr.parser.XPathParser.parse(XPathParser.java:472) at net.sf.saxon.expr.parser.ExpressionTool.make(ExpressionTool.java:98) at net.sf.saxon.xpath.XPathEvaluator.compile(XPathEvaluator.java:194) at net.sf.saxon.xpath.XPathEvaluator.evaluate(XPathEvaluator.java:319)
I get the same exception with Saxon 9.7 when trying to run the ApplyXPathJAXP from the Saxon samples in the resources zip collection for Saxon 9.7 which also simply returns null in the getPrefixes method so I am not sure how to set up a NamespaceContext that works with 9.7.
Replies (3)
Please register to reply
RE: Using JAXP XPath API with NamespaceContext fails in 9.7 while it works in 9.6 - Added by Michael Kay over 8 years ago
Looks superficially the same as bug #2711 - but that one is specific to .Net.
RE: Using JAXP XPath API with NamespaceContext fails in 9.7 while it works in 9.6 - Added by Michael Kay over 8 years ago
It's working for me on my current build so I think it's a problem we have already fixed, but I have yet to pin down the exact history.
RE: Using JAXP XPath API with NamespaceContext fails in 9.7 while it works in 9.6 - Added by Michael Kay over 8 years ago
It is indeed bug #2711. I misread the description. 2711 was a bug on the Java platform caused by changes that we made which should only have affected the .NET build, but accidentally affected the Java build as well. So it will be fixed in the next maintenance release, which we are testing at the moment.
Please register to reply