我想要一些有关使用saxon解析默认名称空间的xpath的信息.我正在使用Saxon-HE-9.5.1-3.jar在代码中使用xpath 2功能.在类路径中包含saxon库之后,我面临着解析具有默认名称空间的XML文档的xpath的问题.
我正在使用示例XML:
100
有效的XPATH:/ RESPONSE / HEADER / HITS
在某些情况下,它可以工作,而不能工作:
>“ XPATH Works”:未指定名称空间时
示例:< RESPONSE responseCode =“ 200”>
>“ XPATH Works”:给定带前缀的名称空间时
示例:< RESPONSE xmlns:res =“ http://www.abc.com/” responseCode =“ 200”>
>“ XPATH无效”:当给出没有前缀的默认名称空间时示例:< RESPONSE xmlns =“ http://www.abc.com/” responseCode =“ 200”>
您能帮我解决一下为什么Saxon不以不同的方式对待命名空间和默认命名空间吗?
另外,我该如何解决对具有默认名称空间的文档执行xpath的情况.
以下是我的代码块:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import net.sf.saxon.xpath.XPathEvaluator;
import net.sf.saxon.xpath.XPathFactoryImpl;
.
.
.
DocumentBuilder builder;
Document doc;
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
builder = domFactory.newDocumentBuilder();
doc = builder.parse(new ByteArrayInputStream(b, 0, size));
XPathFactory factory = XPathFactoryImpl.newInstance(XPathConstants.DOM_OBJECT_MODEL);
XPathEvaluator xpathCompiler = (XPathEvaluator) factory.newXPath();
XPathExpression expr = xpathCompiler.compile(xpath);
NodeList childNodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
谢谢&问候
普拉塔普