Class DomXPathRule
- java.lang.Object
-
- net.sourceforge.pmd.properties.AbstractPropertySource
-
- net.sourceforge.pmd.lang.rule.AbstractRule
-
- net.sourceforge.pmd.lang.xml.rule.DomXPathRule
-
- All Implemented Interfaces:
PropertySource,Rule
public class DomXPathRule extends AbstractRule
XPath rule that executes an expression on the DOM directly, and not on the PMD AST wrapper. The XPath expressions adheres to the XPath (2.0) spec, so they can be tested in any existing XPath testing tools instead of just the PMD designer (google "xpath test"). Usage of this class is strongly recommended over the standardXPathRule, which is mostly useful in other PMD languages.Differences with
This rule andXPathRuleXPathRuledo not accept exactly the same queries, becauseXPathRuleimplements the XPath spec in an ad-hoc way. The main differences are:XPathRuleuses elements to represent text nodes. This is contrary to the XPath spec, in which element and text nodes are different kinds of nodes. To replace the query//elt/text[@Image="abc"], use the XPath functiontext(), eg//elt[text()="abc"].XPathRuleadds additional attributes to each element (eg@BeginLineand@Image). These attributes are not XML attributes, so they are not accessible using DomXPathRule rule. Instead, use the XPath functionspmd:startLine(node),pmd:endLine(node)and related. For instance, replace//elt[@EndLine - @BeginLine > 10]withelt[pmd:endLine(.) - pmd:startLine(.) > 10].XPathRuleuses an element called"document"as the root node of every XML AST. This node does not have the correct node kind, as it's an element, not a document. To replace/document/RootNode, use just/RootNode.XPathRuleignores comments and processing instructions (eg FXML's<?import javafx.Node ?>). This rule makes them accessible with the regular XPath syntax. The following finds all comments in the file:
The following finds only top-level comments starting with "prefix"://comment()
Note the use of/comment()[fn:starts-with(fn:string(.), "prefix")]fn:string. As an example of matching processing instructions, the following fetches all<?import ... ?>processing instructions.
The string value of the instruction can be found with/processing-instruction('import')fn:string.
Additionally, this rule only supports XPath 2.0, with no option for configuration. This will be bumped to XPath 3.1 in PMD 7.
Namespace-sensitivity
Another important difference is that this rule is namespace-sensitive. If the tested XML documents use a schema (
xmlnsattribute on the root), you should set the propertydefaultNsUrion the rule with the value of thexmlnsattribute. Otherwise node tests won't match unless you use a wildcard URI prefix (*:nodeName).For instance for the document
the XPath query<foo xmlns="http://company.com/aschema"> </foo>//foowill not match anything, while//*:foowill. If you set the propertydefaultNsUrito"http://company.com/aschema", then//foowill be expanded to//Q{http://company.com/aschema}foo, and match thefoonode. The behaviour is equivalent in the following document:<my:foo xmlns:my='http://company.com/aschema'> </my:foo>However, for the document
the XPath queries<foo> </foo>//fooand//*:fooboth match, because//foois expanded to//Q{}foo(local name foo, empty URI), and the document has no default namespace (= the empty default namespace).Note that explicitly specifying URIs with
Q{...}localNameas in this documentation is XPath 3.1 syntax and will only be available in PMD 7.- Since:
- PMD 6.44.0
- Author:
- Clément Fournier
-
-
Field Summary
-
Fields inherited from class net.sourceforge.pmd.properties.AbstractPropertySource
propertyDescriptors, propertyValuesByDescriptor
-
Fields inherited from interface net.sourceforge.pmd.Rule
VIOLATION_SUPPRESS_REGEX_DESCRIPTOR, VIOLATION_SUPPRESS_XPATH_DESCRIPTOR
-
-
Constructor Summary
Constructors Constructor Description DomXPathRule()DomXPathRule(String xpath)DomXPathRule(String xpath, String defaultNsUri)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidapply(Node node, RuleContext ctx)voidinitialize(LanguageProcessor languageProcessor)-
Methods inherited from class net.sourceforge.pmd.lang.rule.AbstractRule
addExample, addRuleChainVisit, addViolation, addViolation, addViolation, addViolationWithMessage, addViolationWithMessage, addViolationWithMessage, asCtx, buildTargetSelector, deepCopy, deepCopyValuesTo, end, equals, getDescription, getExamples, getExternalInfoUrl, getLanguage, getMaximumLanguageVersion, getMessage, getMinimumLanguageVersion, getName, getPriority, getPropertySourceType, getRuleClass, getRuleSetName, getSince, getTargetSelector, hashCode, isDeprecated, setDeprecated, setDescription, setExternalInfoUrl, setLanguage, setMaximumLanguageVersion, setMessage, setMinimumLanguageVersion, setName, setPriority, setRuleClass, setRuleSetName, setSince, start
-
Methods inherited from class net.sourceforge.pmd.properties.AbstractPropertySource
copyPropertyDescriptors, copyPropertyValues, definePropertyDescriptor, getOverriddenPropertiesByPropertyDescriptor, getOverriddenPropertyDescriptors, getPropertiesByPropertyDescriptor, getProperty, getPropertyDescriptor, getPropertyDescriptors, hasDescriptor, isPropertyOverridden, setProperty
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.sourceforge.pmd.properties.PropertySource
definePropertyDescriptor, dysfunctionReason, getOverriddenPropertiesByPropertyDescriptor, getOverriddenPropertyDescriptors, getPropertiesByPropertyDescriptor, getProperty, getPropertyDescriptor, getPropertyDescriptors, hasDescriptor, isPropertyOverridden, setProperty
-
-
-
-
Method Detail
-
apply
public void apply(Node node, RuleContext ctx)
-
initialize
public void initialize(LanguageProcessor languageProcessor)
-
-