diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/DocumentNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/DocumentNode.java index 23d99fb778..65c5ecec8b 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/DocumentNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/DocumentNode.java @@ -95,6 +95,8 @@ public class DocumentNode extends BaseNodeInfo implements DocumentInfo, AstNodeO return new Navigator.DescendantEnumeration(this, true, true); case Axis.CHILD: return SingleNodeIterator.makeIterator(rootNode); + case Axis.SELF: + return SingleNodeIterator.makeIterator(this); default: return super.iterateAxis(axisNumber); } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/xpath/SaxonXPathRuleQueryTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/xpath/SaxonXPathRuleQueryTest.java index e2be132092..bfe925bac3 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/xpath/SaxonXPathRuleQueryTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/xpath/SaxonXPathRuleQueryTest.java @@ -64,6 +64,24 @@ public class SaxonXPathRuleQueryTest { Assert.assertEquals(dummy, result.get(0)); } + @Test + public void testRootExpressionIsADocumentNode() { + DummyNode dummy = new DummyNode(); // todo in pmd 7 this should be a RootNode + + List result = assertQuery(1, "(/)[self::document-node()]", dummy); + Assert.assertEquals(dummy, result.get(0)); + } + + @Test + public void testRootExpressionWithName() { + DummyNode dummy = new DummyNode(0, false, "DummyNodeA"); // todo in pmd 7 this should be a RootNode + + List result = assertQuery(1, "(/)[self::document-node(element(DummyNodeA))]", dummy); + Assert.assertEquals(dummy, result.get(0)); + + assertQuery(0, "(/)[self::document-node(element(DummyNodeX))]", dummy); + } + @Test public void ruleChainVisits() { SaxonXPathRuleQuery query = createQuery("//dummyNode[@Image='baz']/foo | //bar[@Public = 'true'] | //dummyNode[@Public = false()] | //dummyNode");