xsl:decimal-format

  1. decimal-format_1-input.xml

  2. <?xml version="1.0"?>
  3. <products>
  4.   <product id="p1" price="3250" stock="4" country="Denmark"/>
  5.   <product id="p2" price="1000" stock="5" country="Germany"/>
  6.   <product id="p3" price="1200" stock="19" country="Germany"/>
  7.   <product id="p4" price="1500" stock="5" country="Australia"/>
  8.   <product id="p5" price="1225" stock="3" country="Japan"/>
  9. </products>
  1. decimal-format_1-stylesheet.xsl

  2. <?xml version="1.0"?>
  3. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  4.   <xsl:decimal-format grouping-separator="," decimal-separator="." name="us"/>
  5.   <xsl:decimal-format grouping-separator="." decimal-separator="," name="dk"/>
  6.   <xsl:output indent="yes"/>
  7.   <xsl:template match="/">
  8.     <product>
  9.       <xsl:for-each select="products/product">
  10.         <xsl:choose>
  11.           <xsl:when test="@country = 'Denmark'">
  12.             <product id="{@id}" price="{format-number(@price, 'DKK #.##0,00', 'dk')}" country="{@country}"/>
  13.           </xsl:when>
  14.           <xsl:otherwise>
  15.             <product id="{@id}" price="{format-number(@price, 'USD #,##0.00', 'us')}" country="{@country}"/>
  16.           </xsl:otherwise>
  17.         </xsl:choose>
  18.       </xsl:for-each>
  19.     </product>
  20.   </xsl:template>
  21. </xsl:stylesheet>
  1. decimal-format_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <product>
  4.   <product id="p1" price="DKK 3.250,00" country="Denmark"/>
  5.   <product id="p2" price="USD 1,000.00" country="Germany"/>
  6.   <product id="p3" price="USD 1,200.00" country="Germany"/>
  7.   <product id="p4" price="USD 1,500.00" country="Australia"/>
  8.   <product id="p5" price="USD 1,225.00" country="Japan"/>
  9. </product>

Comment

Defaults: grouping-separator="," and decimal-separator=".".

Updated 2009-03-19