If you're not worried about efficiency, then
<xsl:template match="a">
<b number="{count((.|preceding-sibling::a)[not(@val)]) +
sum((.|preceding-sibling::a)/@val)}"/>
</xsl:template>
For a more efficient solution you need a recursive template that walks over the siblings accumulating a running total as it goes.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference