XPath test to determine the type of node

I do not understand why this test

count(.|../@*)=count(../@*) 

(from Dave Pawson 's Home Page )

identify the node attribute: (

can someone give me a detailed explanation?

+4
source share
3 answers

A few things to understand:

  • . refers to the current node (aka "context node")
  • the node attribute has a parent element (to which it belongs)
  • the XPath join operation (with | ) never duplicates nodes, i.e. (.|.) leads to one node, not two
  • there is an axis self:: , which you could use theoretically (for example, self::* works to find out if the element is a node), but self::@* does not work, so we should use something else

Knowing this, you can say:

  • ../@* retrieves all attributes of the current parent node (all attributes are "sibling") if you do)
  • (.|../@*) combines the current node with them - if the current node is an attribute, the total counter does not change (according to No. 3 above)
  • therefore, if count(.|../@*) is equal to count(../@*) , the current node must be a node attribute .
+5
source

Just for completeness, in XSLT 2.0 you can do

 <xsl:if test="self::attribute()">...</xsl:if> 
+2
source

Here is how it works

 count( # Count the nodes in this set .|../@*) # include self and all attributes of the parent # this counts all of the distinct nodes returned by the expression # if the current node is an attribute, then it returns the count of all the # attributes on the parent element because it does not count the current # node twice. If it is another type of node it will return 1 because only # elements have attribute children =count( # count all the nodes in this set ../@*) # include all attribute nodes of the parent. If the current node is not an # attribute node this returns 0 because the parent can't have attribute # children 
+1
source

All Articles