Vlax
Vlax

Reputation: 1537

XSLT 2.0: <xsl:number> conditional formatting

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}"/>

Upvotes: 1

Views: 309

Answers (2)

Vlax
Vlax

Reputation: 1537

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}"/>

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 117140

Try:

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

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

Upvotes: 2

Related Questions