Vlax
Vlax

Reputation: 1527

XSLT 2.0: <xsl:number> "initialization"

Is there a way to "initialize" the sequence the xsl:number element produces?

For instance if number will produce the following output:

1  
1.1  
1.1.1

I would like that initializing it to 2 for instance it will produce the following result:

2  
2.1  
2.1.1

and so on...

Is there any chance or do I have to create my own function/template to achieve that?

Upvotes: 1

Views: 461

Answers (3)

Abel
Abel

Reputation: 57169

This actually is likely to make it into the new XSLT 3.0 specification. There is a public bug report, see https://www.w3.org/Bugs/Public/show_bug.cgi?id=27060, which, if accepted (and it looks that way) will give you more freedom in expression xsl:number start-at values, per level.

Of course, this would require you to use an XSLT 3.0 processor.

Upvotes: 0

Vlax
Vlax

Reputation: 1527

Well actually I didn't think that would work but it does seems to work:

for this example input xml:

<root>
    <item/>
    <item>
        <item/>
    </item>
    <item/>
    <item/>
    <item/>
</root>

and using this XSLT to test the transformation:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <result>
            <xsl:apply-templates/>
        </result>
    </xsl:template>
    <xsl:template match="item">
        <xsl:variable name="currItem">
            <xsl:number count="item" level="multiple"/>
        </xsl:variable>
        <item numer="{$currItem+1}"/>
        <xsl:apply-templates/>
    </xsl:template>
</xsl:stylesheet>

you can achieve that. Note the $currItem+1, that's where the initialization takes place.

so the result is:

<?xml version="1.0" encoding="UTF-8"?>
<result>
    <item numer="2"/>
    <item numer="3"/>
    <item numer="3.1"/>
    <item numer="4"/>
    <item numer="5"/>
    <item numer="6"/>
</result>

Regards

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167581

I think http://www.w3.org/TR/xslt-30/#number has a start-at attribute but you would need to use Saxon 9.6 PE or EE and XSLT 3.0 for that.

Upvotes: 1

Related Questions