It's impossible to achieve the goal using xsl:number instruction. Here is the stylesheet:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates>
<xsl:with-param name="level" select="''"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*">
<xsl:param name="level"/>
<xsl:variable name="curr-name" select="name()"/>
<xsl:variable name="siblings-and-self" select="../*"/>
<xsl:variable name="unique-element-names">
<xsl:call-template name="unique-element-names">
<xsl:with-param name="node-set" select="$siblings-and-self"/>
</xsl:call-template>
</xsl:variable>
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:variable name="current-unique-element-pos" select="count($unique-element-names/*[name() = $curr-name]/preceding-sibling::*) + 1"/>
<xsl:variable name="lvl">
<xsl:choose>
<xsl:when test="not($level)">1</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($level, '.', $current-unique-element-pos)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:attribute name="element-level">
<xsl:value-of select="$lvl"/>
</xsl:attribute>
<xsl:apply-templates>
<xsl:with-param name="level" select="$lvl"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="node()[self::text() or self::comment() or self::processing-instruction()]">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template name="unique-element-names">
<xsl:param name="node-set"/>
<xsl:choose>
<xsl:when test="$node-set">
<xsl:variable name="first-item" select="$node-set[1]"/>
<xsl:element name="{name($first-item)}">
<xsl:value-of select="name($first-item)"/>
</xsl:element>
<xsl:call-template name="unique-element-names">
<xsl:with-param name="node-set" select="$node-set[name() != name($first-item)]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
And the XML source, for example, is(it's now well-formed):
Code:
<?xml version="1.0" encoding="utf-8"?>
<Z>
<A>
<B>xxx</B>
<C>
<C1>xxx</C1>
xxx
</C>
<C>
<C2>xxx</C2>
xxx
</C>
<D>xxx</D>
</A>
<E>
<F>
<G>xxx</G>
<G>xxx</G>
<H>xxx</H>
</F>
</E>
</Z>
The stylesheet performs an identity transformation, except that it
appends "element-level" attribute (containing the needed numbering) to each element.
Regards,
Armen