title: Controlling processing order for correct updating of values
body: <p>Using a large XML corpus, I am integrating two sets of XSL 3.0 code for building footnotes found at 
  <a href="https://stackoverflow.com/questions/48123059/xsl-numbering-using-two-different-sets-simultaneously">XSL numbering using two different sets simultaneously</a> (creates numbers) and at <a href="https://stackoverflow.com/questions/48132710/xsl-getting-value-of-immediately-preceding-element-attribute">XSL getting value of immediately preceding element/@attribute</a> (creates notes) .</p>
  
  <p>The combined code is found here: <a href="http://xsltfiddle.liberty-development.net/948Fn5a/24" rel="nofollow noreferrer">http://xsltfiddle.liberty-development.net/948Fn5a/24</a></p>
  
  <p>Below is the XML and XSL 3.0 found on the xsltfiddle linked above, processed by Saxon. I will post the problem immediately:</p>
  
  <p>With the XSL <code>&lt;xsl:value-of select=". || '=' || preceding-sibling::*[1]/@n"/&gt;</code> one expects the number or letter in the <code>appnote</code> / <code>footnote</code> to be returned in the block <code>footnotes</code>. The contents of <code>preceding-sibling</code> are being returned correctly, so evidently the XPATH is targeting  the correct element(s).</p>
  
  <p>Is this an issue of order of processing within XSL?  That somehow the <code>footnote</code> is created, then <code>footnote-block</code> created, and finally <code>footnote</code> receives its respective number/letter? Does this require another <code>mode</code>?</p>
  
  <p>XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;corpus&gt;
    &lt;deposition&gt;
      &lt;deposition-title&gt;Praesent vitae&lt;/deposition-title&gt;
      &lt;text&gt;
          &lt;seg n="seg1" type="not_foo"&gt;Lorem ipsum dolor sit amet, consectetur 
              adipiscing elit. Vivamus&lt;note2 n="abc"&gt;another note 2&lt;/note2&gt; ultrices consequat facilisis. 
              Suspendisse a odio&lt;note n="def"&gt;foo note&lt;/note&gt; in lobortis. Aenean 
              non dui scelerisque, rutrum est at, cursus sem.&lt;/seg&gt;
          &lt;seg n="seg2" type="foo"&gt;Ut pharetra bibendum ipsum, portitor 
              velit pharetra quis. Aeneano&lt;note n="ghi"&gt;foo note&lt;/note&gt; purus. Praesent 
              aliquam viverra tellus&lt;note n="jkl"&gt;another note&lt;/note&gt; in condimentum.&lt;/seg&gt;
      &lt;/text&gt;
    &lt;/deposition&gt;
    &lt;deposition&gt;
      &lt;deposition-title&gt;Elementum arcu non&lt;/deposition-title&gt;
      &lt;text&gt;
          &lt;seg n="seg1" type="foo"&gt;Curabitur pulvinar leo eget. Orci varius 
              natoque penatibus et magnis dis&lt;note n="mno"&gt;foo note&lt;/note&gt; montes, 
              nascetur ridiculus mus.&lt;/seg&gt;
          &lt;seg n="seg2" type="foo"&gt;Curabitur pulvinar leo eget. Orci varius 
              natoque penatibus&lt;note2  n="pqr"&gt;another note 2&lt;/note2&gt; et 
              magnis dis&lt;note n="stu"&gt;foo note&lt;/note&gt; montes, 
              nascetur ridiculus mus.&lt;/seg&gt;
          &lt;seg n="seg3" type="not_foo"&gt;Morbi vehicula dolor bibendum enim mollis lobortis. 
              Nulla rutrum vel diam vel posuere. Aliquam pellentesque 
              malesuada elit sed tempor.&lt;/seg&gt;
      &lt;/text&gt;
    &lt;/deposition&gt;
    &lt;deposition&gt;
      &lt;deposition-title&gt;Elementum arcu non&lt;/deposition-title&gt;
      &lt;text&gt;
          &lt;seg n="seg1" type="foo"&gt;Curabitur pulvinar leo eget. Orci varius 
              natoque penatibus et magnis dis&lt;note n="vwx"&gt;foo note&lt;/note&gt; montes, 
              nascetur ridiculus mus.&lt;/seg&gt;
          &lt;seg n="seg2" type="not_foo"&gt;Morbi vehicula dolor bibendum enim mollis lobortis. 
              Nulla rutrum vel diam vel posuere. Aliquam&lt;note2  n="yz"&gt;another note 2&lt;/note2&gt; pellentesque 
              malesuada elit sed tempor.&lt;/seg&gt;
      &lt;/text&gt;
    &lt;/deposition&gt;
    &lt;/corpus&gt;
  </code></pre>
  
  <p>XSL which adds footnote numbers and then creates a 'block' of notes at bottom of <code>deposition</code>: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs"
  version="3.0"&gt;
  
  &lt;xsl:mode on-no-match="shallow-copy"/&gt;
  
  &lt;!-- ADD FOOTNOTE NUMBERING, IN TWO DIFFERENT SETS: 1, 2, 3... and a, b, c... --&gt;
  &lt;xsl:mode name="add-notes" on-no-match="shallow-copy"/&gt;
  
  &lt;xsl:variable name="notes"&gt;
    &lt;xsl:apply-templates mode="add-notes"/&gt;
  &lt;/xsl:variable&gt;
  
  &lt;xsl:template match="seg[@type = 'foo'] | note" mode="add-notes"&gt;
    &lt;xsl:next-match/&gt;
    &lt;footnote/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="note2" mode="add-notes"&gt;
    &lt;xsl:next-match/&gt;
    &lt;appnote/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="/"&gt;
    &lt;xsl:apply-templates select="$notes/node()"/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="footnote"&gt;
    &lt;xsl:copy&gt;
        &lt;xsl:number level="any" format="1" from="deposition"/&gt;
    &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="appnote"&gt;
    &lt;xsl:copy&gt;
        &lt;xsl:number level="any" format="a" from="deposition"/&gt;
    &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;!-- CREATE FOOTNOTE BLOCK FROM NUMBERING ABOVE --&gt;
  &lt;xsl:template match="deposition"&gt;
    &lt;deposition&gt;
      &lt;xsl:apply-templates/&gt; 
      &lt;footnote-block&gt;
          &lt;xsl:for-each select="text//footnote"&gt;
              &lt;footitem&gt;
                  &lt;xsl:value-of select=". || '=' || preceding-sibling::*[1]/@n"/&gt;
              &lt;/footitem&gt;
          &lt;/xsl:for-each&gt;
           &lt;xsl:for-each select="text//appnote"&gt;
              &lt;appitem&gt;
                  &lt;xsl:value-of select=". || '=' || preceding-sibling::*[1]/@n"/&gt;
              &lt;/appitem&gt;
          &lt;/xsl:for-each&gt;
      &lt;/footnote-block&gt;
    &lt;/deposition&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p></p>