title: Checking parent's preceding siblings XSLT
body: <p>Given:</p>
  
  <pre><code>&lt;pm&gt;
      &lt;content&gt;
          &lt;pmEntry&gt;
              &lt;pmEntryTitle&gt;Test&lt;/pmEntryTitle&gt;
              &lt;dmRef&gt;
                  &lt;dmRefIdent&gt;
                      &lt;dmCode infoCode="100" infoCodeVariant="A" /&gt;
                  &lt;/dmRefIdent&gt;
              &lt;/dmRef&gt;
          &lt;/pmEntry&gt;
          &lt;pmEntry&gt;
              &lt;pmEntryTitle&gt;Test2&lt;/pmEntryTitle&gt;
              &lt;dmRef&gt;
                  &lt;dmRefIdent&gt;
                      &lt;dmCode infoCode="200" infoCodeVariant="A" /&gt;
                  &lt;/dmRefIdent&gt;
              &lt;/dmRef&gt;
          &lt;/pmEntry&gt;
          &lt;pmEntry&gt;
              &lt;pmEntryTitle&gt;Test3&lt;/pmEntryTitle&gt;
              &lt;dmRef&gt;
                  &lt;dmRefIdent&gt;
                      &lt;dmCode infoCode="300" infoCodeVariant="A" /&gt;
                  &lt;/dmRefIdent&gt;
              &lt;/dmRef&gt;
          &lt;/pmEntry&gt;
      &lt;/content&gt;
  &lt;/pm&gt;
  </code></pre>
  
  <p>I want to call the Table of Contents template from the first <code>&lt;dmRef&gt;</code> in the first <code>&lt;pmEntry&gt;</code></p>
  
  <p>But this is not giving the expected output, it's sometimes calling the TOC again from the middle of the list of pmEntry as well as from the first pmEntry.</p>
  
  <pre><code>&lt;xsl:if test="ancestor::content[count(pmEntry) &gt; 1]/pmEntry and count(../preceding-sibling::pmEntry) = 0"&gt;
      &lt;xsl:call-template name="contents" /&gt;
  &lt;/xsl:if&gt;
  </code></pre>
  
  <p>I don't understand why <code>count(../preceding-sibling::pmEntry)</code> is returning 0 in the middle of the list of pmEntries.</p>
  
  <p>Any help or suggestions are always appreciated.</p>
  
  <p>This is the complete template:</p>
  
  <pre><code> &lt;xsl:template match="pmEntry/dmRef"&gt;
                 &lt;!-- Front Matter template infocode = 001 before TOC--&gt;
    &lt;xsl:choose&gt;
              &lt;xsl:when test="ancestor::content/pmEntry/dmRef/dmRefIdent/dmCode[@infoCode='001']"&gt;
                      &lt;xsl:apply-templates/&gt;
                      &lt;xsl:if test="dmRefIdent/dmCode/@infoCode='001'"&gt;
                          &lt;xsl:call-template name="contents" /&gt;
                          &lt;xsl:call-template name="figlist" &gt;
                          &lt;/xsl:call-template&gt;
                      &lt;/xsl:if&gt;
                  &lt;/xsl:when&gt;
                  &lt;xsl:otherwise&gt;
                      &lt;xsl:choose&gt;
                      &lt;!--Only one pmEntry, at first dmRef create TOC --&gt;
                          &lt;xsl:when test="ancestor::content[count(pmEntry) = 1]/pmEntry and count(preceding-sibling::dmRef) = 0"&gt;
                              &lt;xsl:call-template name="contents" /&gt;
                              &lt;xsl:call-template name="figlist" /&gt;
                          &lt;/xsl:when&gt;
                          &lt;!--Multiple pmEntry, at first create TOC --&gt;
                          &lt;xsl:when test="count(../preceding-sibling::pmEntry)=0 and count(../preceding-sibling::dmRef)=0"&gt;
                              &lt;xsl:call-template name="contents" /&gt;
                              &lt;xsl:call-template name="figlist" /&gt;
                          &lt;/xsl:when&gt;
                          &lt;xsl:otherwise /&gt;
                      &lt;/xsl:choose&gt;
                      &lt;xsl:apply-templates/&gt;
                  &lt;/xsl:otherwise&gt;
              &lt;/xsl:choose&gt;       
             &lt;/xsl:template&gt;
  </code></pre>