9.4 Inserting an Individual Formatted Value

The value attribute of the number elements can be an individual value that you can format and then insert into the output. As a single value, it will also be a fixed value that isn't based on the position of the current node in the source document.

However, the value attribute can contain an expression whose result is a number that is not fixed. If, for example, the expression in value consists only of the position( ) function, numbering will be sequential and not fixed. See the stylesheet value.xsl in examples/ch09 for an example of this (not shown here). You can apply this stylesheet to canada.xml if you want to try it. This stylesheet also sorts the content of canada.xml in reverse, or descending, order.

If you want to insert the single number 1,000,000 into a result tree, you could do so with the value attribute on number. Given the little document thanks.xml:

<thank>Thanks a </thanks>

you could insert a single number into it with thanks.xsl, as shown in Example 9-15.

Example 9-15. Inserting a single formatted number
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>      <xsl:template match="thanks">   <xsl:value-of select="."/>   <xsl:number value="1000000" grouping-size="3" grouping-separator=","/>   <xsl:text>!</xsl:text>  </xsl:template>     </xsl:stylesheet>

The value attribute holds the desired number, 1000000. The grouping-size attribute indicates that you want to group the number at the thousands place. The grouping-separator attribute specifies a separator character (a comma [,]) that will occur at the thousands place. In order to work, the two grouping attributes must be used together.

Process thanks.xml with thanks.xsl for this result:

Thanks a 1,000,000!

Without the two grouping attributes used on number in thanks.xsl, the commas wouldn't appear in this output.

If you live in a locale that uses a period or dot (.) instead of a comma (,) as a group separator, you will prefer to use the stylesheet in Example 9-16, dot.xsl.

Example 9-16. Formatting the number with a dot separator
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>      <xsl:template match="thanks">   <xsl:value-of select="."/>   <xsl:number value="1000000" grouping-size="3" grouping-separator="."/>   <xsl:text>!</xsl:text>  </xsl:template>     </xsl:stylesheet>

The only difference between thanks.xsl and dot.xsl is the value of the grouping-separator attribute. When used against thanks.xml, this stylesheet generates:

Thanks a 1.000.000!

The grouping attributes grouping-size and grouping-separator also work with numbers generated in the ordinary way by the number element, not just with a number supplied by the value attribute, as shown in dot.xsl. For example, if you have a document that has several thousand nodes that you want to count, the following instance of the number element with no value attribute would place a comma at the thousands place:

<xsl:number format="&#10;" grouping-size="3" grouping-separator=","/>

If you want to see this in action, generate the numbers 1 through 2,000 by applying generator.xsl to generator.xml, which contains 2,000 num nodes (not shown here but available in examples/ch09). These files a trivial pair exist among the examples only to demonstrate how the grouping attributes work with ordinary numbering. As you might have guessed, listing 2,000 nodes is impractical to print in a book!



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