使用 Saxon Xpath 2.0 得不到任何结果
Don't get any result using Saxon Xpath 2.0
我想使用 xpath 2.0 或 3.0 表达式解析 xml。
我想使用 XPath 的最新版本,所以我下载了 Saxon jar。
这是我的代码:
Processor proc = new Processor(false);
DocumentBuilder builder = proc.newDocumentBuilder();
XdmNode doc = builder.build(new File(file.getPath()));
XPathCompiler xpath = proc.newXPathCompiler();
XPathSelector selector = xpath.compile(mappingXPath.get(key)).load();
selector.setContextItem(doc);
for (XdmItem item : selector) {
XdmNode node = (XdmNode) item;
org.w3c.dom.Node element = (org.w3c.dom.Node) node.getExternalNode();
System.out.println(element.getTextContent());
}
例如,xpath 表达式 "//mods/identifier[@type="doi"]" for xml:
<collection>
<mods xmlns="http://www.loc.gov/mods/v3" xmlns:etd="http://www.ntltd.org/standards/metadata/etdms/1.0/etdms.xsd" xmlns:local="http://idea.library.drexel.edu" xmlns:mods="http://www.loc.gov/mods/v3" xmlns:datacite="https://schema.datacite.org/meta/kernel-4.0/metadata.xsd" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink">
<identifier type="assetId">16</identifier>
<identifier type="doi">10.17918/D8VD4T</identifier>
</mods>
</collection>```
my selector is empty, why I don't get any result while I run the same expression and xml file in https://www.freeformatter.com/xpath-tester.html
and it provide results?
如果 XDM 节点是 wrapper/view 外部节点(例如 DOM 节点),XdmNode.getExternalNode()
只会 return 结果。使用 Saxon DocumentBuilder
构建的节点是原生 XDM 节点,而不是外部 DOM 节点的视图。如果你想在 Saxon 中使用 DOM,你可以 - 只需在外部构建 DOM 节点并使用 DocumentBuilder.wrap(domNode)
包装它。但请注意,Saxon 在处理 DOM 节点时比使用其原生 XDM 树模型时慢 5 到 10 倍。
除了 Mike 在他的回答中写的内容,如果您有 XdmNode
node
并且想要它的字符串内容,您可以使用 node.getStringValue()
,结果应该是 getTextContent()
会给你一个 DOM 节点。
方法 getStringValue
的文档位于 https://www.saxonica.com/html/documentation10/javadoc/net/sf/saxon/s9api/XdmItem.html#getStringValue--。
我想使用 xpath 2.0 或 3.0 表达式解析 xml。 我想使用 XPath 的最新版本,所以我下载了 Saxon jar。 这是我的代码:
Processor proc = new Processor(false);
DocumentBuilder builder = proc.newDocumentBuilder();
XdmNode doc = builder.build(new File(file.getPath()));
XPathCompiler xpath = proc.newXPathCompiler();
XPathSelector selector = xpath.compile(mappingXPath.get(key)).load();
selector.setContextItem(doc);
for (XdmItem item : selector) {
XdmNode node = (XdmNode) item;
org.w3c.dom.Node element = (org.w3c.dom.Node) node.getExternalNode();
System.out.println(element.getTextContent());
}
例如,xpath 表达式 "//mods/identifier[@type="doi"]" for xml:
<collection>
<mods xmlns="http://www.loc.gov/mods/v3" xmlns:etd="http://www.ntltd.org/standards/metadata/etdms/1.0/etdms.xsd" xmlns:local="http://idea.library.drexel.edu" xmlns:mods="http://www.loc.gov/mods/v3" xmlns:datacite="https://schema.datacite.org/meta/kernel-4.0/metadata.xsd" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink">
<identifier type="assetId">16</identifier>
<identifier type="doi">10.17918/D8VD4T</identifier>
</mods>
</collection>```
my selector is empty, why I don't get any result while I run the same expression and xml file in https://www.freeformatter.com/xpath-tester.html
and it provide results?
XdmNode.getExternalNode()
只会 return 结果。使用 Saxon DocumentBuilder
构建的节点是原生 XDM 节点,而不是外部 DOM 节点的视图。如果你想在 Saxon 中使用 DOM,你可以 - 只需在外部构建 DOM 节点并使用 DocumentBuilder.wrap(domNode)
包装它。但请注意,Saxon 在处理 DOM 节点时比使用其原生 XDM 树模型时慢 5 到 10 倍。
除了 Mike 在他的回答中写的内容,如果您有 XdmNode
node
并且想要它的字符串内容,您可以使用 node.getStringValue()
,结果应该是 getTextContent()
会给你一个 DOM 节点。
方法 getStringValue
的文档位于 https://www.saxonica.com/html/documentation10/javadoc/net/sf/saxon/s9api/XdmItem.html#getStringValue--。