Enumerating ancestors in XSLT I've been playing with some genealogical files in XML lately and wanted to create a transform that would list, for each person, the person's name, the count of ancestors, and the list of ancestors.  Here's what I came up with, but I'd like to see if there's a better way to do this.
&lt;?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?&gt;
&lt;xsl:stylesheet version=&quot;1.0&quot; 
    xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
    xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;
&gt;
&lt;xsl:output method=&quot;text&quot;/&gt;

&lt;xsl:template match=&quot;/people&quot;&gt;
    &lt;xsl:apply-templates select=&quot;person&quot;/&gt;
&lt;/xsl:template&gt;

&lt;!-- for each person, print the name and ancestor count --&gt;
&lt;xsl:template match=&quot;person&quot;&gt;
    &lt;xsl:value-of select=&quot;@name&quot;/&gt;
    &lt;xsl:text&gt;(&lt;/xsl:text&gt;
    &lt;xsl:apply-templates select=&quot;.&quot; mode=&quot;count-kin&quot;/&gt;
    &lt;xsl:text&gt;): &lt;/xsl:text&gt;
    &lt;!-- now name all of the ancestors --&gt;
    &lt;xsl:apply-templates select=&quot;.&quot; mode=&quot;name-kin&quot;/&gt;
    &lt;xsl:text&gt;
&lt;/xsl:text&gt;
&lt;/xsl:template&gt;

&lt;!-- recursively name the ancestors --&gt;
&lt;xsl:template match=&quot;parent&quot; mode=&quot;name-kin&quot;&gt;
    &lt;xsl:value-of select=&quot;concat(@name,' ')&quot;/&gt;
    &lt;xsl:variable name=&quot;myname&quot; select=&quot;@name&quot;/&gt;
    &lt;xsl:apply-templates select=&quot;/people/person[@name=$myname]&quot; mode=&quot;name-kin&quot;/&gt;
&lt;/xsl:template&gt;

&lt;!-- recursively name the ancestors --&gt;
&lt;xsl:template match=&quot;person&quot; mode=&quot;name-kin&quot;&gt;
    &lt;xsl:apply-templates select=&quot;parent&quot; mode=&quot;name-kin&quot;/&gt;
&lt;/xsl:template&gt;


&lt;!-- recursively count the number of ancestors --&gt;
&lt;xsl:template match=&quot;parent&quot; mode=&quot;count-kin&quot;&gt;
    &lt;xsl:variable name=&quot;myname&quot; select=&quot;@name&quot;/&gt;
    &lt;xsl:variable name=&quot;ancestor-count&quot;&gt;
    &lt;xsl:apply-templates select=&quot;/people/person[@name=$myname]&quot; mode=&quot;count-kin&quot;/&gt;
    &lt;/xsl:variable&gt;
    &lt;xsl:value-of select=&quot;$ancestor-count&quot;/&gt;
&lt;/xsl:template&gt;

&lt;!-- recursively count the number of ancestors --&gt;
&lt;xsl:template match=&quot;person&quot; mode=&quot;count-kin&quot;&gt;
    &lt;xsl:choose&gt;
    &lt;!-- only count ancestors if there are any --&gt;
    &lt;xsl:when test=&quot;count(parent)&quot;&gt;
        &lt;xsl:variable name=&quot;ancestor-count&quot;&gt;
        &lt;xsl:apply-templates select=&quot;parent&quot; mode=&quot;count-kin&quot;/&gt;
        &lt;/xsl:variable&gt;
        &lt;xsl:value-of select=&quot;number($ancestor-count)+count(parent)&quot;/&gt;
    &lt;/xsl:when&gt;
    &lt;!-- no parents --&gt;
    &lt;xsl:otherwise&gt;
        &lt;xsl:value-of select=&quot;0&quot;/&gt;
    &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;
&lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;

Here's a sample input file:
&lt;?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?&gt;
&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;

Here's sample output from xsltproc on Linux:
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
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