| title | Writing XPath rules | ||
|---|---|---|---|
| tags |
|
||
| summary | This page describes XPath rule support in more details | ||
| last_updated | April 2024 (7.2.0) | ||
| permalink | pmd_userdocs_extending_writing_xpath_rules.html | ||
| author | Miguel Griffa <[email protected]>, Clément Fournier <[email protected]> |
{% jdoc_nspace :coremx core::lang.metrics %} {% jdoc_nspace :coreast core::lang.ast %} {% jdoc_nspace :jmx java::lang.java.metrics %} {% jdoc_nspace :jast java::lang.java.ast %}
This page describes some points of XPath rule support in more details. See also the tutorial about how to write a first (XPath) rule.
PMD uses XPath 3.1 for its XPath rules since PMD 7. Before then, the default version was XPath 1.0, with opt-in support for XPath 2.0.
See the Saxonica documentation for an introduction to new features in XPath 3.1.
The property version of {% jdoc core::lang.rule.xpath.XPathRule %} is deprecated and
has been removed with PMD 7.
XPath rules view the AST as an XML-like DOM, which is what the XPath language is defined on. Concretely, this means:
- Every AST node is viewed as an XML element
- The element has for local name the value of {% jdoc core::lang.ast.Node#getXPathNodeName() %} for the given node
- Some Java getters are exposed as XML attributes on those elements
- This means, that documentation for attributes can be found in our Javadocs. For
example, the attribute
@SimpleNameof the Java nodeEnumDeclarationis backed by the Java getter {% jdoc java::lang.java.ast.ASTTypeDeclaration#getSimpleName() %}.
- This means, that documentation for attributes can be found in our Javadocs. For
example, the attribute
To represent attributes, we must map Java values to XPath Data Model (XDM)
values. In the following table we refer to the type conversion function as conv, a function from Java types
to XDM types.
Java type T |
XSD type conv(T) |
|---|---|
int |
xs:integer |
long |
xs:integer |
double |
xs:decimal |
float |
xs:decimal |
boolean |
xs:boolean |
String |
xs:string |
Character |
xs:string |
Enum<E> |
xs:string (uses Object::toString) |
Collection<E> |
conv(E)* (a sequence type) |
The same conv function is used to translate rule property values to XDM values.
Additionally, PMD's own net.sourceforge.pmd.lang.document.Chars is also translated to a xs:string
PMD provides some language-specific XPath functions to access semantic information from the AST.
The namespace of custom PMD functions must be explicitly mentioned.
{% render %} {% include custom/xpath_fun_doc.html %} {% endrender %}