This section provides hands-on examples of XPath queries over the AST. You will probably find this section more useful if you follow it with Designer and copy/paste the examples.
Let's assume you want to match something on class variable names. You see in the ASTVviewer that VariableDeclaratorId contains the variable name - in XML terms, the name is in the @Image attribute. So you try an XPath expression as follows:
//VariableDeclaratorId
If you try this expression you'll see that variables declared in methods are also matched. A more precise expression for matching field declarations is, well, using the FieldDeclaration node. This expression matches only the two fields declared in the class:
//FieldDeclaration
In a similar way, you can match only local variables with this expression
//LocalVariableDeclaration
With local variables we need to be more careful. Consider the following class: