The xsl:decimal-format Element: Creating Numeric Formats

The <xsl:decimal-format> Element: Creating Numeric Formats

Before finishing up with the XSLT and XPath functions, Ill take a look at a special XSLT element, <xsl:decimal-format> , whose sole purpose is to work with a particular function: format-number . In particular, you use this element to define characters and symbols that the format-number function should use. This element has several attributes:

  • name (optional). Name of this decimal format. Set to a QName. If you dont supply a format, the default decimal format is used.

  • decimal-separator (optional). Sets the character that is placed between the integer and fractional part of values. The default is .. Set to a character.

  • grouping-separator (optional). Sets the character that is placed between groups of digits. The default is ,. Set to a character.

  • infinity (optional). Sets the string used to indicate positive infinity. The default is Infinity. Set to a string.

  • minus-sign (optional). Sets the character that represents a minus sign. The default is -. Set to a character.

  • NaN (optional). Sets the string used to represent the Not a Number value. The default is NaN. Set to a string.

  • percent (optional). Sets the character used to represent the percent sign. The default is %. Set to a character.

  • per-mille (optional). Sets the character used to represent per-mille, that is, per-thousand. The default is . Set to a character.

  • zero-digit (optional). Sets the character to be used in format strings to specify where you want a leading or trailing zero. The default is 0. Set to a character.

  • digit (optional). Sets the character to be used in format strings where you want a digit. The default value is 0. Set to a character.

  • pattern-separator (optional). Sets the character used to separate patterns for positive and negative numbers . The default is ;. Set to a character.

This element is a top-level element, and its always empty. Using this element, you can set the formatting characters used by format-number . An xsl:decimal-format without a name attribute set becomes the default decimal format. It is an error to have more that one default xsl:decimal-format element or to have multiple xsl:decimal-format elements with the same name. In the following example, I use European numeric formats to format the numbers in planets.xmlspecifically, I use a comma rather than a decimal point to separate the integer from the fractional values, and a period rather than a comma to separate thousands. All I need to do is specify the new formatting with <xsl:decimal-format> and then put it to work in format-number :

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:decimal-format decimal-separator="," grouping-separator="."/>      <xsl:template match="/PLANETS">          <HTML>              <HEAD>                  <TITLE>                      The Formatted Planets Table                  </TITLE>              </HEAD>              <BODY>                  <H1>                      The Formatted Planets Table                  </H1>                  <TABLE BORDER="2">                      <TR>                          <TD>Name</TD>                          <TD>Mass</TD>                          <TD>Radius</TD>                          <TD>Day</TD>                      </TR>                      <xsl:apply-templates/>                  </TABLE>              </BODY>          </HTML>      </xsl:template>      <xsl:template match="PLANET">         <TR>            <TD><xsl:value-of select="NAME"/></TD>            <TD><xsl:apply-templates select="MASS"/></TD>            <TD><xsl:apply-templates select="RADIUS"/></TD>            <TD><xsl:apply-templates select="DAY"/></TD>         </TR>     </xsl:template>      <xsl:template match="MASS">          <xsl:value-of select="format-number(., '#,###')"/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="RADIUS">          <xsl:value-of select="format-number(., '#.###')"/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="DAY">          <xsl:value-of select="format-number(., '###,##')"/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>  </xsl:stylesheet> 

Heres the result document:

 <HTML>      <HEAD>          <TITLE>              The Formatted Planets Table          </TITLE>      </HEAD>      <BODY>          <H1>              The Formatted Planets Table          </H1>          <TABLE BORDER="2">              <TR>                  <TD>Name</TD>                  <TD>Mass</TD>                  <TD>Radius</TD>                  <TD>Day</TD>              </TR>              <TR>                  <TD>Mercury</TD>                  <TD>0,055 (Earth = 1)</TD>                  <TD>1.516 miles</TD>                  <TD>58,65 days</TD>              </TR>              <TR>                  <TD>Venus</TD>                  <TD>0,815 (Earth = 1)</TD>                  <TD>3.716 miles</TD>                  <TD>116,75 days</TD>              </TR>              <TR>                  <TD>Earth</TD>                  <TD>1 (Earth = 1)</TD>                  <TD>2.107 miles</TD>                  <TD>1 days</TD>              </TR>          </TABLE>      </BODY>  </HTML> 

You can see this result document in Figure 8.3.

Figure 8.3. Setting decimal formats.
graphics/08fig03.gif

And thats all it takesnow you can set the formatting options for the format-number function.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

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