简体   繁体   English

Java XPath返回单个结果而不是NodeSet

[英]Java XPath returns single result instead of NodeSet

I am trying to create an XPath expression in Java (8, default XPath implementation). 我试图在Java中创建一个XPath表达式(8,默认的XPath实现)。 I am doing the following: 我正在执行以下操作:

Object res = xpath.evaluate("(//*[local-name()='PartyId'])", requestDom, XPathConstants.NODESET);

I have multiple PartyId nodes in the document at the same level, because it's parent is repeating. 我在同一级别的文档中有多个PartyId节点,因为它的父级在重复。 I got my result, but only a single node. 我得到了结果,但是只有一个节点。 (the first). (首先)。

Side info: if I write [$k] at the end of the expression, like [1], or [2], I got my elements, but I need all of them. 附带信息:如果我在表达式的末尾写[$ k],例如[1]或[2],则得到了元素,但我需要所有元素。 :( :(

However, if I am testing the very same XPath for example at http://www.freeformatter.com/xpath-tester.html I get multiple results which is the expected result. 但是,如果例如在http://www.freeformatter.com/xpath-tester.html测试相同的XPath,则会得到多个结果,这是预期的结果。 Any ideas? 有任何想法吗?

ps I tried to put Saxon on the classpath but it completely breaks my application (Spring-Boot WS). ps我试图将Saxon放在类路径上,但这完全破坏了我的应用程序(Spring-Boot WS)。

Thanks a lot! 非常感谢!

UPDATE I failed to correctly check the result and it was absolutely correct. 更新我无法正确检查结果,它是绝对正确的。

My guess is that you make a mistake while processing the result NodeList . 我的猜测是您在处理结果NodeList犯了一个错误。 Try the following approach: 请尝试以下方法:

NodeList results = (NodeList) xpath.evaluate(..);
for (int i = 0; i < nodelist.getLength(); i++) {
    Node node = (Node) nodelist.item(i);
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM