a namespace compromises an xpath evaluation - how do i fix it.
Added by john fairless about 11 years ago
I'm evaluating an expression to find missing data but The presence of a namespace is compromising the test. What modification do I have to do to make the 2nd case work the same as the first case.
case 1
String xml = "North"; String xpathExpression = "building/room/not(wall)"; XPath xPath = XPathFactory.newInstance().newXPath(); List result = (List) xPath.compile(xpathExpression).evaluate( new InputSource(new StringReader(xml)), XPathConstants.NODESET);
returns [false] as expected - i.e. populated array
case 2
String xml = "North"; String xpathExpression = "building/room/not(wall)"; XPath xPath = XPathFactory.newInstance().newXPath(); List result = (List) xPath.compile(xpathExpression).evaluate( new InputSource(new StringReader(xml)), XPathConstants.NODESET);
returns [] as not as expected - i.e. empty array
Thanks in anticipation, any ideas would be appreciated - Thanks - sorry if its school boy stuff!
Replies (3)
Please register to reply
RE: a namespace compromises an xpath evaluation - how do i fix it. - Added by john fairless about 11 years ago
fixed:
String xml = "North"; String xpathExpression = "b:building/b:room/not(b:wall)"; XPath xPath = XPathFactory.newInstance().newXPath(); xPath.setNamespaceContext( new NamespaceContext() { public String getNamespaceURI(String prefix) { if ("b".equals(prefix)) { return "abc"; } else { return "some other namespace as required"; } } public String getPrefix(String arg0) {return null; public Iterator getPrefixes(String arg0) {return null;} }); List result = (List) xPath.compile(xpathExpression).evaluate( new InputSource(new StringReader(xml)), XPathConstants.NODESET);
RE: a namespace compromises an xpath evaluation - how do i fix it. - Added by Michael Kay about 11 years ago
Sorry, you got to the answer before I did.
I would encourage you to use the s9api XPath interface rather than JAXP. The JAXP interfaces don't support the XPath 2.0 data types ("NODESET" is not really ideal when the result is a sequence of booleans); it's very DOM-oriented; there's a lot of silliness like the two unused methods in the NamespaceContext; and the mechanism for binding variables and functions is very clumsy in the Saxon implementation because information isn't cleanly split between compile time and evaluation time. Also the JAXP factory interface you are using is very slow, and risks loading an XPath implementation that doesn't support the XPath 2.0 expressions you are using.
RE: a namespace compromises an xpath evaluation - how do i fix it. - Added by john fairless about 11 years ago
Thanks, That detail is appreciated
Please register to reply