The format attribute of the number element isn't the only place to turn for help with formatting numbers in XSLT. You can also use the format-number( ) function coupled optionally with the decimal-format element. The top-level decimal-format element has 10 attributes that define number characteristics, such as the decimal separator and percent sign used when formatting a number. Table 9-1 lists these 10 attributes with their default values.
The number characteristics defined by decimal-format are used with the format-number( ) function. The decimal-format element has no effect unless used with the format-number( ) function. Example 9-28, the document format.xml, provides a list of eight positive integers that will be formatted in this example. Example 9-28. Integers for formatting<?xml version="1.0"?> <?xml-stylesheet href="format.xsl" type="text/xsl"?> <format> <number>100</number> <number>1000</number> <number>10000</number> <number>100000</number> <number>1000000</number> <number>10000000</number> <number>100000000</number> <number>1000000000</number> </format> The XML stylesheet PI references the format.xsl stylesheet shown in Example 9-29. Example 9-29. A stylesheet with named formats for numbers<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:decimal-format name="de" decimal-separator="," grouping-separator="."/> <xsl:decimal-format name="fr" decimal-separator="," grouping-separator=" "/> <xsl:decimal-format name="ru" decimal-separator="," grouping-separator=" "/> <xsl:decimal-format name="uk" decimal-separator="." grouping-separator=","/> <xsl:decimal-format name="us" decimal-separator="." grouping-separator=","/> <xsl:template match="convert"> <html> <head> <title>Number Formatter</title> <style type="text/css"> table {margin-left:auto;margin-right:auto} td {text-align:right;padding: 5px 5px 5px 5px} h3 {text-align:center} </style> </head> <body> <h3>Number Formatter</h3> <table rules="all"> <thead> <tr> <th>Deutschland</th> <th>France</th> <th>Россия</th> <! Russia > <th>United Kingdom</th> <th>United States</th> </tr> </thead> <tbody> <xsl:apply-templates select="number"/> </tbody> </table> </body> </html> </xsl:template> <xsl:template match="number"> <tr> <td><xsl:value-of select="format-number(.,'.###,00€','de')"/></td> <td><xsl:value-of select="format-number(.,' ###,00€','fr')"/></td> <td><xsl:value-of select="format-number(.,' ###,00p.','ru')"/></td> <td><xsl:value-of select="format-number(.,'£,###.00','uk')"/></td> <td><xsl:value-of select="format-number(.,'$,###.00','us')"/></td> </tr> </xsl:template> </xsl:stylesheet> Each of the five instances of the decimal-format element at the top of the stylesheet define a number format, each with its own name. These formats define currency patterns for Germany (Deutschland), France, The stylesheet creates some HTML and CSS for the result tree. The headings (th) include the name Russia spelled in Cyrillic format-number(.,'.###,00€','de') The format-number( ) function can take three arguments (as this call does), but only two arguments are required. The first argument in this call is a period (.). This is a synonym for the current node (current( ) and self::node( ) also work here). The current node is a node from the node list containing all the number nodes in the source tree. The second argument is a number pattern for formatting the number, as follows:
The third and final argument for format-number( ) references a named number format (as in de) that is defined by a decimal-format element. The result of formatting format.xml with format.xsl is shown in Figure 9-1 in Mozilla Firebird. One reason I did this example in HTML is so that I could show the Cyrillic characters and currency symbols. They don't show well in a command prompt window! Figure 9-1. Displaying format.xml in Mozilla Firebird![]() I researched the currency formats using IBM's open source International Components for Unicode (ICU) project. ICU provides libraries of services that use the latest versions of Unicode, including international number formats (see http://oss.software.ibm.com/icu/). For information on these currency patterns discussed here, check out the ICU LocaleExplorer at http://oss.software.ibm.com/cgi-bin/icu/lx/en/utf-8/.
|