Skip to main content

XSLT <xsl:number>

XSLT <xsl:number>

The <xsl:number> element is used to determine the integer position of the current node in the source. It is also used to format a number.

Syntax

<xsl:number  
count="expression"
level="single|multiple|any"
from="expression"
value="expression"
format="formatstring"
lang="languagecode"
letter-value="alphabetic|traditional"
grouping-separator="character"
grouping-size="number"/>

Attributes

AttributeValueRequired/OptionalDescription
countexpressionOptionalAn XPath expression that specifies what nodes are to be counted
levelsingle multiple anyOptionalControls how the sequence number is assigned Can be one of the following:
  • single (default)
  • multiple
  • any (not supported by Netscape 6)
fromexpressionOptionalAn XPath expression that specifies where the counting will start
valueexpressionOptionalSpecifies a user-provided number that is used in place of a sequence generated number
formatformatstringOptionalDefines the output format for the number. Can be one of the following:
  • format="1" results in 1 2 3 ..
  • format="01" results in 01 02 03 (not supported by Netscape 6)
  • format="a" results in a b c ... (not supported by Netscape 6
  • format="A" results in A B C... (not supported by Netscape 6)
  • format="i" results in i ii iii iv ... (not supported by Netscape 6)
  • format="I" results in I II III IV ... (not supported by Netscape 6)
langlanguagecodeOptionalSpecifies the language alphabet to be used for the numbering (Not supported by Netscape 6)
letter-valuealphabetic traditionalOptionalSpecifies whether the numbering in the selected language is alphabetic or traditional. The default is alphabetic
grouping-separatorcharacterOptionalSpecifies what character should be used to separate groups of digits. The default is the comma
grouping-sizenumberOptionalSpecifies how many digits are in the groups that are being separated by the character specified in the grouping-separator attribute. The default is 3

Example

Let's see some example:

Example 1

<xsl:number value="120000" grouping-separator="."/>
120.000

Example 2

<xsl:number value="250000" grouping-size="2"/>
25,00,00

Example 3

<xsl:number value="12" grouping-size="1" grouping-separator="#" format="I"/>
X#I#I

Example 4

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<p>
<xsl:for-each select="bookstore/book">
<xsl:number value="position()" format="1" />
<xsl:value-of select="title" /><br />
</xsl:for-each>
</p>
</body>
</html>
</xsl:template>

</xsl:stylesheet>