XPath to search for an item based on another XPath item

I have a Java AST, and I'm trying to find a variable inside it through XPath.

Suppose a variable is called "foobar", I could use

//VariableDeclarator/VariableDeclaratorId[@Image='foobar']

but what if I don’t know the text "foobar", but I want to read it from another element

//VariableDeclarator/VariableDeclaratorId[@Image=//SynchronizedStatement/Expression/PrimaryExpression/PrimaryPrefix/Name]

The "name" node contains the information "foobar" in @Image, but PrimaryPrefix/Name[@Image]does not work.

How do I rewrite a condition //SynchronizedStatement/Expression/PrimaryExpression/PrimaryPrefix/Namethat it matches @Image='foobar'?

thank

+3
source share
1 answer

Try this XPath: -

//VariableDeclarator/VariableDeclaratorId[@Image=//SynchronizedStatement/Expression/PrimaryExpression/PrimaryPrefix/Name/@Image]
+2
source

Source: https://habr.com/ru/post/1738458/


All Articles