1

I have a client that depending on the number of total nodes present wants to vary the format for a xsl:number element.

For example if the total number of nodes < 101 then the format string is "01" so that 1 => will be displayed 01.

If the total number of nodes > 100 or < 1001 then the string format is "001" then 1 => 001, 99 => 099.

I have tried using a variable in the format attribute to xsl:number but I only got for the format string "001" as a result 10 => $10 instead of 010.

Is there a way to do this without using some xsl:choose for the possible ranges?

EDIT:

Here again the solution I found thanks to Michael's code hint:

 <xsl:variable name="total-number-nodes" select="count(//node)"/>
 <xsl:variable name="base-format-string" select="string('000000000000000000000001')"/>
 <xsl:variable name="fomat-string" select="substring($base-format-string, string-length($base-format-string) - string-length(string($total-number-dossiers)) + 1)"/>
 <xsl:number level="multiple" count="node" format="{$fomat-string}"/>
Vlax
  • 1,447
  • 1
  • 18
  • 24
  • You say "I have tried using a variable in the format attribute to xsl:number" - can you edit the question to show exactly what you have tried? The `format` attribute is treated as an AVT so you can include calculated parts in curly braces like `format="{$var}"` – Ian Roberts Mar 25 '15 at 16:53
  • @IanRoberts Thanks that was part of the problem! – Vlax Apr 02 '15 at 08:22

2 Answers2

2

Try:

<xsl:number format="{substring('000', 1, string-length(string($n)))}"/>

where $n is a variable holding the count of the nodes.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Thanks Michael that put me in the right track although the solution proposed didn't do what I wanted I was able to modify it to achieve it. – Vlax Apr 02 '15 at 08:23
  • @Vlax "*the solution proposed didn't do what I wanted*" In what way? – michael.hor257k Apr 02 '15 at 11:49
  • sorry for the delay to answer I need to test it again but I had the impression it wasn't working but I'll come with an example soon – Vlax Apr 16 '15 at 15:12
0

I can't somehow edit the original question so here the final code I used, probably there's a more elegant solution using translate instead of using substring....

    <xsl:variable name="total-number-nodes" select="count(//node)"/>
    <xsl:variable name="base-format-string" select="string('000000000000000000000001')"/>
    <xsl:variable name="fomat-string" select="substring($base-format-string, string-length($base-format-string) - string-length(string($total-number-dossiers)) + 1)"/>
    <xsl:number level="multiple" count="node" format="{$fomat-string}"/>
Vlax
  • 1,447
  • 1
  • 18
  • 24