title: Counting previous Attributes with a specific value in XSLT
body: <p>I apologize ahead of time if this isn't properly formatted, but...</p>
  
  <p>I need to take the following xml:</p>
  
  <pre><code>&lt;box r="?" c="?" b="?"&gt;
    &lt;h r="?" b="2"&gt;
      &lt;u&gt;
        &lt;v c="5" b="2"&gt;
          &lt;l&gt;
            &lt;h r="?" b="1"/&gt;
          &lt;/l&gt;
          &lt;r&gt;
            &lt;v c="?" b="?"/&gt;
          &lt;/r&gt;
        &lt;/v&gt;
      &lt;/u&gt;
      &lt;d&gt;
        &lt;h r="?" b="2"/&gt;
      &lt;/d&gt;
    &lt;/h&gt;
  &lt;/box&gt;
  </code></pre>
  
  <p>and using xslt:</p>
  
  <pre><code>&lt;xsl:template name="BoxVariables-Count"&gt;
    &lt;xsl:text&gt;
          // Counting...&lt;/xsl:text&gt;
  
    &lt;xsl:apply-templates select="descendant::*[@* = &amp;quot;?&amp;quot;]" mode="box-variable-count"/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="local:*" mode="box-variable-count"&gt;
    &lt;xsl:variable name="position" select="position()"/&gt;
    &lt;xsl:text&gt;
          // &lt;/xsl:text&gt;&lt;/xsl:text&gt;&lt;xsl:value-of select="local-name()"/&gt;
    &lt;xsl:text&gt; = &lt;/xsl:text&gt;
    &lt;xsl:value-of select="count((ancestor::local:box[1]/descendant-or-self::*[@* = &amp;quot;?&amp;quot;][position() &amp;lt; $position + 1])/@*[@* = &amp;quot;?&amp;quot;])"/&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>count the number of "?" attribute entries that occur before the current node in the box element. (As a note: the "?" is actually "Unknown")</p>
  
  <p>The following line outputs the total number of attributes within nodes containing "?"s before the current node:</p>
  
  <pre><code>&lt;xsl:value-of select="count((ancestor::local:box[1]/descendant-or-self::*[@* = &amp;quot;?&amp;quot;][position() &amp;lt; $position + 1])/@*)"/&gt;
  </code></pre>
  
  <p>outputs:</p>
  
  <pre><code>  // h = 3
    // h = 5
    // v = 7
    // h = 9
  </code></pre>
  
  <p>BUT, I want to only count the number of "?" attributes within that set.</p>
  
  <pre><code>&lt;xsl:value-of select="count((ancestor::local:box[1]/descendant-or-self::*[@* = &amp;quot;?&amp;quot;][position() &amp;lt; $position + 1])/@*[@* = &amp;quot;?&amp;quot;])"/&gt;
  </code></pre>
  
  <p>outputs:</p>
  
  <pre><code>  // h = 0
    // h = 0
    // v = 0
    // h = 0
  </code></pre>
  
  <p>and I want:</p>
  
  <pre><code>  // h = 3
    // h = 4
    // v = 5
    // h = 7
  </code></pre>
  
  <p>In other words:</p>
  
  <pre><code>count(./@*[@* = &amp;quot;?&amp;quot;])
  </code></pre>
  
  <p>seems to return 0 and not the number of attributes within self that are set to "?".</p>
  
  <p>It would be nice if I didn't have to write a recursive ? counter...</p>