number

The number element count items sequentially and inserts a formatted number into the result tree. It is not convenient to give all the various conditions that this element allows, so for a more expansive description see the W3C document entry.

Syntax
<number> ::= “number” “{” ( “level” ( “single” | “multiple” | “any” ) | “count” <quote> <pattern> <quote> | “format” <quote> <pattern> <quote> | “from” <quote> <pattern> <quote> | “value” <quote> <number expression> <quote> | “letter-value” ( “alphabetic” | “traditional” ) | “lang” <quote> <language identifier> <quote> | “grouping-separator” <quote> <grouping character> <quote> | “grouping-size” <quote> <digits> <quote> )* “}”
Options

None.

Elements

The range of values within an number block is identical to the XSLT definition. This is a very general list and the formal specification should be consulted for a complete description.

count An XPath that defines what in the source tree should be numbered sequentially.
level Defines how levels of the source tree should be considered in generating sequential numbers.
from Specifies where the numbering should start.how levels of the source tree should be considered in generating sequential numbers.
value Applies a given format to a number.
format Defines the format of the generated number: "1", "01", etc.
lang The language specifier.
letter-value Provides clarification for languages that have more than one numbering system that uses letters.
grouping-separator Specifies the character to be placed between groups of digits (eg thousands). The default is ".".
grouping-size The size of the groups of digits that make up a number. Defaults to 3 representing thousands.
Examples

Using an example based on the W3C description, consider outputting a new list with the number of an item inserted as a Roman digit.

stylesheet { version "1.0" match using "items" { element "newList" { foreach "item" { sort using "." element "p" { number { value "position()" letter-value alphabetic format "1. " } value "." } } } } }

compiles to

<?xml version="1.0" encoding="UTF-8"?> <stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0"> <template match="items"> <element name="newList"> <for-each select="item"> <sort select="."/> <element name="p"> <number value="position()" letter-value="alphabetic" format="i. "/> <value-of select="."/> </element> </for-each> </element> </template> </stylesheet>

Running this against the XML scrap.

<?xml version="1.0" encoding="UTF-8"?> <items> <item>First item</item> <item>Second item</item> <item>Third item</item> <item>Fourth item</item> </items>

would give the result which would sorted alhabetically, so the item Fourth item would be placed second in the list.

<?xml version="1.0" encoding="UTF-8"?> <newList> <p>i. First item</p> <p>ii. Fourth item</p> <p>iii. Second item</p> <p>iv. Third item</p> </newList>
Errors

Most of the errors here are self-explanatory. For example

stylesheet { version "1.0" match using "items" { element "newList" { foreach "item" { sort using "." element "p" { number { vaue "position()" letter-value alphabetic format "1. " } value "." } } } } }

will give the error

**** (106) Unexpected symbol "vaue" in line 10 Check spelling, did you mean "value" "value" ?

Misspelling the keyword

stylesheet { version "1.0" match using "items" { element "newList" { foreach "item" { sort using "." element "p" { nuber { vaue "position()" letter-value alphabetic format "1. " } value "." } } } } }

will give the error, plus some consequential ones

**** (106) Unexpected symbol "nuber" in line 9 Check spelling, did you mean "number" ? **** (106) Unexpected symbol "letter-value" in line 11 Check spelling, no suggestion. **** (106) Unexpected symbol "alphabetic" in line 11 Check spelling, no suggestion. **** (106) Unexpected symbol "format" in line 12 Check spelling, no suggestion. **** (136) Unmatched brackets in 1 Too many open brackets?
Copyright 2024 Hugh Field-Richards. All Rights Reserved.