anonnona
anonnona

Reputation: 23

Why doesn't //* return the document node?

I am trying to understand the following example

<?xml version="1.0" encoding="UTF-8"?>

<c> 
  <a> 
    <b att1="5">
        <c/>
    </b>  
    <d/> 
  </a>  
  <a att1="10"> 
    <d> 
      <c/> 
    </d>  
    <b/> 
  </a> 
</c>

Now I run the XPath query

//*[c]

which I take to mean "All nodes that have a child that is a c". However, this returns only the <b> and <d> nodes that have a <c> child without returning the Document node as I expected. Can anyone explain why?

Upvotes: 2

Views: 46

Answers (1)

har07
har07

Reputation: 89285

Because //* equivalent to /descendant-or-self::node()/*. Notice that the document node referenced by self::node() in the previous XPath, so the outer most node selected by that XPath would be the child of the document node (due to /*), which is the root element c, which doesn't have direct child c, hence didn't get selected.

You want /descendant-or-self::node()[c] to include the document node, which is equivalent to //.[c], see the demo.

Upvotes: 3

Related Questions