title: Building a 'table of contents'
body: <p>Consider the following 'sample.xml'</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;root&gt;
  &lt;level&gt;
      &lt;name&gt;testA&lt;/name&gt;
      &lt;level&gt;
          &lt;name&gt;testB&lt;/name&gt;  
      &lt;/level&gt;
      &lt;level&gt;
          &lt;name&gt;testC&lt;/name&gt;  
          &lt;level&gt;
              &lt;name&gt;testD&lt;/name&gt;  
              &lt;level&gt;
                  &lt;name&gt;testE&lt;/name&gt;  
              &lt;/level&gt;
          &lt;/level&gt;
      &lt;/level&gt;
  &lt;/level&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>Using xmlstarlet i can do:</p>
  
  <pre><code>xml sel -t -m //level -v name -o " " -v "count(ancestor::*)-1" -o "." -v "count(preceding-sibling::*)" -n sample.xml
  </code></pre>
  
  <p>This produces:</p>
  
  <pre><code>testA 0.0
  testB 1.1
  testC 1.2
  testD 2.1
  testE 3.1
  </code></pre>
  
  <p>What should i do to get:</p>
  
  <pre><code>testA 0.0
  testB 1.1
  testC 1.2
  testD 1.2.1
  testE 1.2.1.1
  </code></pre>
  
  <p>In this example i only have 4 levels, but this can be more than 4.</p>
  
  <p>I am thinking of some kind of recursion, are there any links available which can explain how to do that?</p>