Format a chosen value as number : number format « XSLT stylesheet « XML

XML
1. CSS Style
2. SVG
3. XML Schema
4. XQuery
5. XSLT stylesheet
Java
XML Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
XML » XSLT stylesheet » number format 
Format a chosen value as number


File: Data.xml

<?xml version="1.0" ?>

<tables>

  <table>
    <table-name>Conference</table-name>
    <number-of-legs>4</number-of-legs>
    <table-top-material type="laminate">A</table-top-material>
    <table-shape>B</table-shape>
    <retail-price currency="USD">1485</retail-price>
  </table>
</tables>


File: Transform.xslt

<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:template match="/">
    <html>
      <head>
        <title>Three Real Tables</title>
      </head>
      <body>
        <table border="0" cellpadding="10">
          <tr>
            <th align="left">Name</th>
            <th align="left">Shape</th>
            <th align="left">Legs</th>
            <th align="left">Material</th>
            <th align="left">Finish</th>
            <th align="left">Price</th>
          </tr>
          <xsl:apply-templates select="/tables/table">
            <xsl:sort select="table-name" />
          </xsl:apply-templates>
        </table>
        <p />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="table">
    <tr>
      <td>
        <xsl:value-of select="table-name" />
      </td>
      <td>
        <xsl:value-of select="table-shape" />
      </td>
      <td>
        <xsl:value-of select="number-of-legs" />
      </td>
      <td>
        <xsl:value-of select="table-top-material/@type" />
      </td>
      <td>
        <xsl:value-of select="table-top-material" />
      </td>
      <td>
        <xsl:apply-templates select="retail-price" />
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="retail-price">
    <div class="tprice">
      <xsl:choose>
        <xsl:when test="@currency = 'USD'">$</xsl:when>
        <xsl:when test="@currency = 'GBP'">?</xsl:when>
        <xsl:when test="@currency = 'EURO'">C</xsl:when>
        <xsl:when test="@currency = 'YEN'">Y</xsl:when>
      </xsl:choose>
      <xsl:value-of select="format-number(., '#,##0.00')" />
    </div>
  </xsl:template>

</xsl:stylesheet>
Output:

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Three Real Tables</title>
    </head>
   <body>
      <table border="0" cellpadding="10">
         <tr>
            <th align="left">Name</th>
            <th align="left">Shape</th>
            <th align="left">Legs</th>
            <th align="left">Material</th>
            <th align="left">Finish</th>
            <th align="left">Price</th>
         </tr>
         <tr>
            <td>Conference</td>
            <td>B</td>
            <td>4</td>
            <td>laminate</td>
            <td>A</td>
            <td>
               <div class="tprice">$1,485.00</div>
            </td>
         </tr>
      </table>
      <p></p>
   </body>
</html>

 
Related examples in the same category
1. Number style
2. Format number after calculation
3. Various number formats
4. number format="001. "
5. number level="multiple" format="1. "
6. number format="1. " level="multiple"
7. number format="1. " level="multiple" count="chapter|sect1"
8. number format="1. " level="multiple" count="chapter|sect1|sect2"
9. number format="1. "
10. number format="1. " level="any"
11. number format="1. " level="any" from="chapter"
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.