Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath test to identify node type

Tags:

types

xpath

I don't understand why this test

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

( from Dave Pawson's Home page )

identify an attribute node :(

could someone give me a detailled explanation ?

like image 447
Patrick Marty Avatar asked Jun 29 '11 13:06

Patrick Marty


1 Answers

A few things to understand:

  1. . refers to the current node (aka "context node")
  2. an attribute node has a parent (the element it belongs to)
  3. an XPath union operation (with |) never duplicates nodes, i.e. (.|.) results in one node, not two
  4. there is the self:: axis you could use in theory (e.g. self::* works to find out if a node is an element), but self::@* does not work, so we must use something different

Knowing that, you can say:

  • ../@* fetches all attributes of the current node's parent (all "sibling attributes", if you will)
  • (.|../@*) unions the current node with them – if the current node is an attribute, the overall count does not change (as per #3 above)
  • therefore, if count(.|../@*) equals count(../@*), the current node must be an attribute node.
like image 165
Tomalak Avatar answered Oct 03 '22 05:10

Tomalak