How would I write an xpath query to match the <subitem> element in the XML snippet below?
Specifically I want to match any element that has a “name” attribute that matches the value of the “name” attribute of the root. There may be an arbitrary number of ancestors between <root> and <subitem>.
<root name="xyz">
<anything>
<subitem name="xyz" />
</anything>
</root>
My goal is to write a PMD rule for finding cases of Logger.getLogger() that use a different class than the class the statement is in.
Use this XPath expression:
This selects any element (including the top element named
root), whosenameattribute has the same string value as that of thenameattribute of the top element. When this XPath expression is evaluated against the provided XML document, two elements —rootandsubitemare selected.I think that only the second element of these two is wanted. For this, use the following XPath expression:
XSLT – based verification:
When this transformation is applied on the provided XML document:
the two XPath expressions are evaluated and the selected nodes are copied to the output: