문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top