title: How to maintain a count within nested elements using XSLT?
body: <p>Related to the question <a href="https://stackoverflow.com/questions/1710179/how-to-use-an-incremented-counter-to-provide-a-unique-id-in-xslt">here</a>.</p>
  
  <p>I have XML input of the form:</p>
  
  <pre><code>&lt;book&gt;
      &lt;chapter&gt;
          ...
      &lt;/chapter&gt;
  &lt;/book&gt;  
  </code></pre>
  
  <p>I am currently outputting something like:</p>
  
  <pre><code>&lt;div class="book" id="book-3"&gt;
      &lt;div class="chapter&gt;
          ...
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Using the rule <code>&lt;div class="book" id="book-{position()}"&gt;</code> to create the a book id. So that the value of the id increments on each <code>&lt;book&gt;</code> node. </p>
  
  <p>I would like output of the form:</p>
  
  <pre><code>&lt;div class="book" id="book-3"&gt;
      &lt;div class="chapter" id="book-3-chapter-7"&gt;
          ...
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I have been trying to use a rule which retrieved the id attribute from the parent and output it in the child. It was only as I was asking this question that I realised of course there would never be a match for <code>"../@id"</code> because the attribute doesn't exist in the source XML, only the output.</p>
  
  <p>Attempting to get the parent attribute to use as part of the id is probably not "the XSLT way". So even if there is a way to access the output, it's probably the wrong way to do this.</p>
  
  <p>Once the chapter tags are created with their id, there is children of the chapter elements I would like to follow the same convention (so they would have <code>&lt;div id="book-2-chapter12-verse212"&gt;</code>). The id will be used by a separate Java application to link in to the document, so it's important that it follows a convention (i.e. generated ids won't work).</p>
  
  <p>What would be the best way to create this nested incremented counter using XSLT?</p>
  
  <p>P.S. The title of the question is probably miles off what I really want to ask, but I don't know XSLT enough to ask the right question. Please feel free to edit, or suggest a better title.</p>
  
  <hr>
  
  <p>EDIT: Although there have been good answers for the original question, as I said, I wasn't really sure how to ask the question properly. Having been given the answer for the question I should have asked, I have modified the question.</p>
  
  <p>This means there are answers which address my (badly-put) original question and an answer which understands the intent I found difficult to express. I have accepted the latter.</p>
  
  <p>I hope the others who answered find this acceptable, and if there are any suggestions for how to handle <em>asking the wrong question in the first place</em>, I'd be glad to hear them.</p>