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






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"