为什么 XPath.selectNodes(context) 总是使用 JDOM 中的整个文档
我尝试在几个不同的上下文中运行相同的查询,但总是得到相同的结果。 这是一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用
"//t"
作为 XPath 表达式,这意味着“查找文档中的所有t
元素”。要仅查找上下文节点中的后代
t
元素,请使用".//t"
。有关更多详细信息,请参阅 XPath 规范的“缩写语法”部分。
You're using
"//t"
as your XPath expression, which means precisely "find allt
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.