XML-specific features and guidance
Table of Contents
Language Info for Maven POM
Language Info for WSDL
Language Info for XML
Language Info for XSL

The XML language module

PMD has an XML language module which exposes the DOM of an XML document as an AST. Different flavours of XML are represented by separate language instances, which all use the same parser under the hood. The following table lists the languages currently provided by the pmd-xml maven module.

Language ID Description
xml Generic XML language
pom Maven Project Object Model (POM)
wsdl Web Services Description Language
xsl Extensible Stylesheet Language

Each of those languages has a separate rule index, and may provide domain-specific XPath functions. At their core they use the same parsing facilities though.

File attribution

Any file ending with .xml is associated with the xml language. Other XML flavours use more specific extensions, like .xsl.

Some XML-based file formats do not conventionally use a .xml extension. To associate these files with the XML language, you need to use the --force-language xml command-line arguments, for instance:

$ pmd check -d /home/me/src/xml-file.ext -f text -R ruleset.xml --force-language xml

Please refer to PMD CLI reference for more examples.

XPath rules in XML

While other languages use XPathRule to create XPath rules, the use of this class is not recommended for XML languages. Instead, since 6.44.0, you are advised to use DomXPathRule. This rule class interprets XPath queries exactly as regular XPath, while XPathRule works on a wrapper for the DOM which is inconsistent with the XPath spec. Since DomXPathRule conforms to the XPath spec, you can

  • test XML queries in any stock XPath testing tool, or use resources like StackOverflow to help you write XPath queries.
  • match XML comments and processing instructions
  • use standard XPath functions like text() or fn:string

Here’s an example declaration of a DomXPathRule:

<rule name="MyXPathRule"
      message="A message"

        <property name="xpath">
            /a/b/c[@attr = "5"]
        <!-- Note: the property "version" is unsupported. -->

The most important change is the class attribute, which doesn’t point to XPathRule but to DomXPathRule. Please see the Javadoc for DomXPathRule for more info about the differences with XPathRule.