<xsl:number> Element

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Reference

<xsl:number> Element

Inserts a formatted number into the result tree.

<xsl:number
  level = "single" | "multiple" | "any"
  count = Pattern
  from = Pattern
  value = number-Expression 
  format = { string }
  lang = { nmtoken }
  letter-value = { "alphabetic" | "traditional" }
  grouping-separator = { char }
  grouping-size = { number } />

Attributes

level
Specifies what levels of the source tree should be considered; it has the values "single", "multiple" and "any". The default is "single".
count
A pattern that specifies what nodes should be counted at those levels. If the count attribute is not specified, then it defaults to the pattern that matches any node with the same node type as the current node and, if the current node has an expanded-name, with the same expanded-name as the current node.
from
A pattern that specifies where counting starts.
value
Specifies the expression to be converted to a number and output to the result tree. If no value attribute is specified, the <xsl:number> element inserts a number based on the position of the current node in the source tree.
format
A sequence of tokens that specifies the format to be used for each number in the list. If there are no format tokens, then the default value of 1 is used which generates a sequence 1 2 … 10 11 12…. Each number after the first is separated from the preceding number by the separator token preceding the format token used to format that number. If there are no separator tokens, a period character is used (".").

Format token Sequence generated
1 1 2 3 4 5 … 10 11 12 …
01 01 02 03 … 19 10 11 … 99 100 101…
A A B C … Z AA AB AC…
i i ii iii iv v vi vii viii ix x…
I I II III IV V VI VII VIII IX X…

lang
Specifies which language's alphabet is used. If no lang value is specified, the language is determined from the system environment.
letter-value
Disambiguates between numbering sequences that use letters. One numbering sequence assigns numeric values to letters in alphabetic sequence, and the other assigns numeric values to each letter in some other manner traditional in that language. In English, these correspond to the numbering sequences specified by the format tokens "a" and "i". In some languages, the first member of each sequence is the same, and so the format token alone would be ambiguous. A value of "alphabetic" specifies the alphabetic sequence; a value of "traditional" specifies the other sequence. Default is "alphabetic".
grouping-separator
Gives the separator used as a grouping (e.g. thousands) separator in decimal numbering sequences. For example, grouping-separator="," and grouping-size="3" would produce numbers of the form 1,000,000. If only one of the grouping-separator and grouping-size attributes is specified, it is ignored.
grouping-size
Specifies the size (normally 3) of the grouping. For example, grouping-separator="," and grouping-size="3" would produce numbers of the form 1,000,000. If only one of the grouping-separator and grouping-size attributes is specified, it is ignored.

Element Information

Number of occurrences Unlimited
Parent elements xsl:copy, xsl:element, xsl:fallback, xsl:for-each, xsl:if, xsl:message, xsl:otherwise, xsl:param, xsl:template, xsl:variable, xsl:when, xsl:with-param, output elements
Child elements (No child elements)

Remarks

The number to be inserted is specified by an expression contained in the value attribute. The expression is evaluated and the resulting object is converted to a number. The number is rounded to an integer and then converted to a string and inserted in the result tree.

The <xsl:number> element first constructs a list of positive integers using the level, count and from attributes:

  • When level="single", it goes up to the first node in the ancestor-or-self axis that matches the count pattern, and constructs a list of length one containing one plus the number of preceding siblings of that ancestor that match the count pattern. If there is no such ancestor, it constructs an empty list. If the from attribute is specified, then the only ancestors that are searched are those that are descendants of the nearest ancestor that matches the from pattern. Preceding-siblings has the same meaning here as with the preceding-sibling axis.
  • When level="multiple", it constructs a list of all ancestors of the current node in document order followed by the element itself; it then selects from the list those nodes that match the count pattern; it then maps each node in the list to one plus the number of preceding siblings of that node that match the count pattern. If the from attribute is specified, then the only ancestors that are searched are those that are descendants of the nearest ancestor that matches the from pattern. Preceding siblings has the same meaning here as with the preceding-sibling axis.
  • When level="any", it constructs a list of length one containing the number of nodes that match the count pattern and belong to the set containing the current node and all nodes at any level of the document that are before the current node in document order, excluding any namespace and attribute nodes (in other words the union of the members of the preceding and ancestor-or-self axes). If the from attribute is specified, then only nodes after the first node before the current node that match the from pattern are considered.

The following languages/numbering schemes are supported. "Format Token" corresponds to the format attribute, "Language" corresponds to the lang attribute, and "Letter-Value" corresponds to the letter-value attribute.

Description Format Token Language Letter-Value
Western 0x0031 (1) n/a n/a
Upper-case letter 0x0041 (A) n/a n/a
Lower-case letter 0x0061 (a) n/a n/a
Upper-case Roman 0x0049 (I) n/a n/a
Lower-case Roman 0x0069 (i) n/a n/a
Upper-case Russian (Cyrillic) 0x0410 n/a n/a
Lower-case Russian (Cyrillic) 0x0430 n/a n/a
Hebrew Alphabetic 0x05d0 n/a Alphabetic
Hebrew Traditional 0x05d0 n/a Traditional
Arabic 0x0623 n/a n/a
Hindi Consonants 0x0905 n/a n/a
Hindi Vowels 0x0915 n/a n/a
Hindi Numbers 0x0967 n/a n/a
Thai Letters 0x0e01 n/a n/a
Thai Numbers 0x0e51 n/a n/a
Japanese Aiueo (double-byte) 0x30a2 n/a n/a
Japanese Iroha (double-byte) 0x30a4 n/a n/a
Korean Chosung 0x3131 n/a n/a
Taiwanese Decimal 0x4e01 "zh-tw" n/a
Korean Decimal 0x4e01 "ko" n/a
Asian Decimal 0x4e01 any other lang n/a
Asian Kanji 0x58f1 n/a n/a
Traditional Taiwanese 0x58f9 "zh-tw" n/a
Traditional Chinese 0x58f9 any other lang n/a
Chinese "Zodiac" 12 0x5b50 n/a n/a
Chinese "Zodiac" 10 0x7532 n/a n/a
Chinese "Zodiac" 60 0x7532, 0x5b50 n/a n/a
Korean Ganada 0xac00 n/a n/a
Korean Decimal 0xc77c n/a n/a
Korean 99 0xd558 n/a n/a
Western (double-byte) 0xff11 n/a n/a
Japanese Aiueo (single-byte) 0xff71 n/a n/a
Japanese Iroha (single-byte) 0xff72 n/a n/a

If the format token alone is enough to disambiguate a particular numbering scheme, there is no need to specify the language or letter-value.

Here are some examples of conversion specifications.

format="&#x30A2;" specifies Katakana numbering.

format="&#x30A4;" specifies Katakana numbering in the "iroha" order.

format="&#x0E51;" specifies numbering with Thai digits.

format="&#x05D0;" letter-value="traditional" specifies traditional Hebrew numbering.

format="&#x10D0;" letter-value="traditional" specifies Georgian numbering.

format="&#x03B1;" letter-value="traditional" specifies classical Greek numbering.

format="&#x0430;" letter-value="traditional" specifies Old Slavic numbering.

Example

This example demonstrates the use of the <xsl:number> element by numbering items in a sorted list with Western and then lower-case Roman numbering.

Note To test this example, you need to use a script. For more information, see Initiate XSLT in a Script.

XML File (items.xml)

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="numelem.xsl" ?>
<items>
   <item>Car</item>
   <item>Pen</item>
   <item>LP Record</item>
   <item>Wisdom</item>
   <item>Cell phone</item>
   <item>Film projector</item>
   <item>Hole</item>
   <item>Canopy</item>
   <item>Widget</item>
   <item>Concept</item>
   <item>Null character</item>
</items>

XSLT File (numelem.xsl)

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

<xsl:template match="items">
   <xsl:for-each select="item">
      <xsl:sort select="."/>
      <xsl:number value="position()" format="1. "/>
      <xsl:value-of select="."/>, 
      <xsl:number value="position()" format="&#x0069;) "/>
      <xsl:value-of select="."/>
      <br/>
   </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Output

This is the formatted output:

1. Canopy, i) Canopy
2. Car, ii) Car
3. Cell phone, iii) Cell phone
4. Concept, iv) Concept
5. Film projector, v) Film projector
6. Hole, vi) Hole
7. LP Record, vii) LP Record
8. Null character, viii) Null character
9. Pen, ix) Pen
10. Widget, x) Widget
11. Wisdom, xi) Wisdom

This is the processor output:

<?xml version="1.0" encoding="UTF-16"?>1. Canopy, 
      i) Canopy<br />2. Car, 
      ii) Car<br />3. Cell phone, 
...
      xi) Wisdom<br />

See Also

Inserting Automatic Numbering Using XSLT