Inside Xslt

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:

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.

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

Категории