XML support | PMD Source Code Analyzer (original) (raw)

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 DOMof 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-specificXPath 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 referencefor 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

Here’s an example declaration of a DomXPathRule:

<rule name="MyXPathRule"
      language="xml"
      message="A message"
      class="net.sourceforge.pmd.lang.xml.rule.DomXPathRule">

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

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