title: XQuery – counting parent’s preceding siblings
body: <p>I wish I could be able to count preceding siblings of the highest <code>div</code> in ePub (for a footnote). I need to pass the value to the attribute before passing notes through XSLT.</p>
  
  <pre><code>for $note in doc('/db/custom_jh/bukwor.xml')//tei:note[@place='bottom']
  let $parent := count($note[preceding-sibling::tei:div[@n='1']])
  let $update := update insert attribute att2 {$parent} into $note
  return $note
  </code></pre>
  
  <p>Attempts with <code>$note[preceding-sibling::tei:div[@n='1']]</code> or <code>$note[ancestor-or-self::tei:div[@n='1']]</code> returns just <code>0</code> or the total sum of all the divs.</p>
  
  <p>Something like <code>&lt;xsl:number level="any" select="tei:div[@n='1']/&gt;"</code> from XSLT, if possible.</p>
  
  <p><strong>UPDATE</strong>
  The very minimal code for counting (still not working, returns only 6 × <code>1</code>, should at least one <code>2</code>:</p>
  
  <pre><code>for $note at $count in doc('/db/custom_jh/bukwor.xml')//tei:note[@place='bottom']
  let $parent := count($note[ancestor-or-self::*/tei:div[@n='1']])
  return $parent
  </code></pre>