Enumerating ancestors in XSLT
Fun question!
Usually when I find myself doing something like this: select=&quot;/people/person[@name=$myname]&quot;, I'll use an xsl:key instead.
You could use that key to collect all of the parents based on the parent person's @name in a variable.
If you could treat that variable as a node-set (either XSLT 2.0+ or EXSLT's node-set() function) getting the count of ancestors would simply be a matter of counting the number of items in the node-set (count()).
Another option, which feels a little hacky in my opinion, is to output a separator along with the ancestor name.
To get the ancestor count you can remove all characters other than the separator character (nested translate()) and then count the number of separators (string-length()).
You'd also need to replace all of the separators with a space when you finally output the ancestor names (another translate() call).
Example...
XML Input
&lt;people&gt;
    &lt;person name=&quot;adam&quot;/&gt;
    &lt;person name=&quot;eve&quot;/&gt;
    &lt;person name=&quot;cain&quot;&gt;
        &lt;parent name=&quot;adam&quot;/&gt;
        &lt;parent name=&quot;eve&quot;/&gt;
    &lt;/person&gt;
    &lt;person name=&quot;henoch&quot;&gt;
        &lt;parent name=&quot;cain&quot;/&gt;
    &lt;/person&gt;
    &lt;person name=&quot;gladys&quot;/&gt;
    &lt;person name=&quot;frank&quot;&gt;
        &lt;parent name=&quot;henoch&quot;/&gt;
    &lt;/person&gt;
    &lt;person name=&quot;jose&quot;&gt;
        &lt;parent name=&quot;gladys&quot;/&gt;
        &lt;parent name=&quot;frank&quot;/&gt;
    &lt;/person&gt;
&lt;/people&gt;

XSLT 1.0
&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
  &lt;xsl:output method=&quot;text&quot;/&gt;
  &lt;xsl:strip-space elements=&quot;*&quot;/&gt;

  &lt;xsl:key name=&quot;parent&quot; match=&quot;parent&quot; use=&quot;../@name&quot;/&gt;

  &lt;xsl:template match=&quot;person&quot;&gt;
    &lt;xsl:variable name=&quot;ancestors&quot;&gt;
      &lt;xsl:apply-templates select=&quot;key('parent',@name)&quot;/&gt;
    &lt;/xsl:variable&gt;
    &lt;xsl:variable name=&quot;ancestor_count&quot; 
      select=&quot;string-length(translate($ancestors,translate($ancestors,'|',''),''))&quot;/&gt;      
    &lt;xsl:value-of select=&quot;concat(@name,'(',$ancestor_count,'): ',translate($ancestors,'|',' '))&quot;/&gt;
    &lt;xsl:text&gt;&amp;#xA;&lt;/xsl:text&gt;
  &lt;/xsl:template&gt;

  &lt;xsl:template match=&quot;parent&quot;&gt;
    &lt;xsl:value-of select=&quot;concat(@name,'|')&quot;/&gt;
    &lt;xsl:apply-templates select=&quot;key('parent',@name)&quot;/&gt;
  &lt;/xsl:template&gt;
  
&lt;/xsl:stylesheet&gt;

Output
adam(0): 
eve(0): 
cain(2): adam eve 
henoch(3): cain adam eve 
gladys(0): 
frank(4): henoch cain adam eve 
jose(6): gladys frank henoch cain adam eve 

Fiddle: http://xsltfiddle.liberty-development.net/3MEdvhm