Quick fix:
Uncomment the inside your <xsl:when test="starts-with(name(), 'level')"> block, otherwise the elements won't nest at all.
Move the <xsl:apply-templates /> after the <xsl:choice>, inside, to the <xsl:otherwise> I suppose.
---
Smarter more durable fix: when you call apply-templates, only select the ones you want, eg.
<xsl:apply-templates select="*[(@a = '1') or not (@a)]/>
|