为什么 XPath.selectNodes(context) 总是使用 JDOM 中的整个文档

发布于 2024-10-15 20:45:58 字数 588 浏览 5 评论 0原文

我尝试在几个不同的上下文中运行相同的查询,但总是得到相同的结果。 这是一个 xml 示例:

<root>
<p>
  <r>
    <t>text</t>
  </r>
</p>
<t>text2</t>
</root>

这就是我正在做的事情:

final XPath xpath = XPath.newInstance("//t");

List<Element> result = xpath.selectNodes(thisIsThePelement); 
// and I've debuged it, it really is the <p> element

并且我总是在结果列表中获得两个 元素。 我只需要传递给 XPath 对象的

内的

任何想法都会有很大帮助,谢谢。

I'm trying to run the same query on several different contexts, but I always get the same result.
This is an example xml:

<root>
<p>
  <r>
    <t>text</t>
  </r>
</p>
<t>text2</t>
</root>

So this is what I'm doing:

final XPath xpath = XPath.newInstance("//t");

List<Element> result = xpath.selectNodes(thisIsThePelement); 
// and I've debuged it, it really is the <p> element

And I always get both <t> elements in the result list.
I need just the <t> inside the <p> I'm passing to the XPath object.

Any ideas would be of great help, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆梦 2024-10-22 20:45:58

您使用 "//t" 作为 XPath 表达式,这意味着“查找文档中的所有 t 元素”。

要仅查找上下文节点中的后代 t 元素,请使用".//t"

有关更多详细信息,请参阅 XPath 规范的“缩写语法”部分。

You're using "//t" as your XPath expression, which means precisely "find all t elements in the document".

To only find the descendant t elements from the context node, use ".//t".

See the "abbreviated syntax" part of the XPath spec for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文