A more accurate way to determine if node $node root of a node :
not(count($node/ancestor::node()))
The expression in TimC's answer checks the type of the current node:
count(.|/)=1
but not applicable if we want to determine the type of node in a variable, which may belong to another document, and not the current document.
In addition, a test for the node namespace :
count($node | $node/../namespace::*) = count($node/../namespace::*)
source share