【发布时间】:2020-02-06 11:58:07
【问题描述】:
我正在尝试使用来自 net.sf.saxon 的带有 Saxonica API 的杂项 xpath 查询 XML 文件,但似乎每次查询操作都返回没有 xml 标记的结果 - 只有内容.有没有办法做到这一点(直接或变通)?
更明确地说:
对于xml文件
<books>
<book lang="en">
<nrpages>140</nrpages>
<author>J.R.R.Tolkien</author>
</book>
</books>
和 xpath
//book
我想找回
<book lang="en">
<nrpages>140</nrpages>
<author>J.R.R.Tolkien</author>
</book>
而不是
140
J.R.R.Tolkien
我尝试过的:
XPathFactory factory = new XPathFactoryImpl();
XPathExpression compiledXPath = factory.newXPath().compile(xPathExpression);
TinyNodeImpl nodeItem = (TinyNodeImpl) compiledXPath.evaluate(new InputSource(filename), XPathConstants.NODE);
nodeItem.atomize(); // brings only the content
nodeItem.getStrinValue(); // brings only the content
【问题讨论】: