9.8 More Help with Formatted Numbers

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 default values of decimal-format are assumed if the element is not present; if decimal-format is present, the default values are assumed if a given attribute is not used.


Table 9-1. Decimal format attributes

Attribute

Default

Description

Example

decimal-separator

.

Symbol that acts as a decimal point

100.00

digit

#

Symbol that represents any digit in number patterns

,###.00

grouping-separator

,

Symbol that separates groups of digits

1,000.00

infinity

Infinity

Symbol that represents infinity

∞ ()

minus-sign

-

Symbol that represents a minus sign

− (-)

name

 

Name for a decimal format

us

NaN

NaN

Symbol for Not a Number

?

pattern-separator

;

Symbol for separating pattern definitions

,###.00;

(-,###.00)

percent

%

Symbol for percent sign

percent

per-mille

Symbol for per mille sign

permille

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>&#x420;&#x43E;&#x441;&#x441;&#x438;&#x44f;</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&#x20AC;','de')"/></td>  <td><xsl:value-of select="format-number(.,' ###,00&#x20AC;','fr')"/></td>  <td><xsl:value-of select="format-number(.,' ###,00p.','ru')"/></td>  <td><xsl:value-of select="format-number(.,'&#xA3;,###.00','uk')"/></td>  <td><xsl:value-of select="format-number(.,'&#x24;,###.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, (Russia), the United Kingdom, and the United States. The currency patterns identify the decimal and grouping separators that are formally used when describing currency in those countries.

The stylesheet creates some HTML and CSS for the result tree. The headings (th) include the name Russia spelled in Cyrillic using character references. As the table rows are formed with the second template, each number element in format.xml is processed with each of the five named number formats by calling the format-number( ) function. I'll pick apart the first function call so you can better understand what's going on with all five:

format-number(.,'.###,00&#x20AC;','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 period (.) represents a grouping separator.

  • The three hashes, or pound signs (###), each represent digits. (You could change this to some other symbol with the digit attribute in decimal-format; the default is #.)

  • The comma (,) after the ### represents a decimal point or separator.

  • The character reference (&#x20AC;) is for the Euro currency symbol figs/u20ac.gif.

  • This pattern can produce a formatted number such as 1.000,00 figs/u20ac.gif.

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
figs/lxsl_0901.gif

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/.

Though it is well-researched, ICU might not always reflect the common street practice of native speakers or users of a given language.




Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net