Jim Cusler
Jim Cusler

Reputation: 23

Can i create an child element counter in XSLT 1.0?

Giving the following source XML, i have processes a and b working (sources and results below) Now, how can i get to the expected exhibit c results below ?

Extra points if it can be done in natural xslt 1.0 without resorting to a JAVA counter!

Any help is appreciated, thanks !

Source XML :

             <A>
                <B/>
                <B/>
                <B/>
            </A>
            <A>
                <B/>
                <B/>
                <B/>
            </A>
            <A>
                <B/>
                <B/>
                <B/>
            </A>

[ ex. 'a' ]

code:

        <xsl:for-each select="A"><xsl:text>&#xA;</xsl:text>
            <xsl:text>A_</xsl:text><xsl:value-of select="position()"/>
            <xsl:for-each select="B"><xsl:text>&#xA;</xsl:text>
                <xsl:text>  B_</xsl:text><xsl:value-of select="position()"/>
            </xsl:for-each>
        </xsl:for-each>

results:

A_1
    B_1
    B_2
    B_3
A_2
    B_1
    B_2
    B_3
A_3
    B_1
    B_2
    B_3

[ ex. 'b' ]

code :

 <xsl:for-each select="A/B"><xsl:text>&#xA;</xsl:text>
        <xsl:text>  B_</xsl:text><xsl:value-of select="position()"/>
    </xsl:for-each>

results:

B_1
B_2
B_3
B_4
B_5
B_6
B_7
B_8
B_9

[ ex. 'c' ]

code: ?????? (this is what i'm looking for)

results (expected) :

A_1
    B_1
    B_2
    B_3
A_2
    B_4
    B_5
    B_6
A_3
    B_7
    B_8
    B_9

Upvotes: 1

Views: 22

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167491

Use <xsl:number level="any"/> instead of position().

Upvotes: 2

Related Questions