1

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Vlax
  • 1,447
  • 1
  • 18
  • 24

3 Answers3

1

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.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • thanks very much for the prompt answer but sadly I'm currently stuck with AltovaXML that only understands XSLT 2.0. Is there any way to achieve that using an XSLT 2.0 engine? – Vlax Oct 15 '14 at 10:38
  • Given that XSLT 3.0 saw a need to introduce that new attribute I think with XSLT 2.0 there is no such feature to easily create a number starting with a certain integer value. I would try to store the `xsl:number` result in a variable and then try to manipulate its components e.g. ``. – Martin Honnen Oct 15 '14 at 10:52
  • I honestly thought the same as you, that I would have to fiddle with the string result tokenizing it, but my answer seems simpler. Please let me know if you see anything that may not work in some case. Thank you so much for your answer anyway, appreciated. – Vlax Oct 15 '14 at 10:56
  • after the comment from @tim-c I realized that your solution is the correct one, I didn't realized the number was being interpreted as of type xs:double on my proposed solution – Vlax Oct 15 '14 at 11:21
0

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

Vlax
  • 1,447
  • 1
  • 18
  • 24
  • 2
    I think this will fail in the case where your nesting is 3 or more levels deep ``. In this case, it will try to add 1 to 3.1.1 and get an error! If you only ever have two levels of nesting in your XML though, it would be OK. – Tim C Oct 15 '14 at 11:06
0

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.

Abel
  • 56,041
  • 24
  • 146
  • 247