XML/XSLT stylesheet/number format

Материал из Web эксперт
Перейти к: навигация, поиск

Format a chosen value as number

   <source lang="xml">

File: Data.xml <?xml version="1.0" ?> <tables>

<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>

</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>
<xsl:apply-templates select="/tables/table"> <xsl:sort select="table-name" /> </xsl:apply-templates>
Name Shape Legs Material Finish Price
       <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">
     <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")" />
 </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>
Name Shape Legs Material Finish Price
Conference B 4 laminate A
$1,485.00

  </body>

</html>

</source>
   
  


Format number after calculation

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <products>

 <product name="strawberry jam">
   <region name="south" sales="20.00" />
   <region name="north" sales="50.00" />
 </product>
 <product name="raspberry jam">
   <region name="south" sales="205.16" />
   <region name="north" sales="10.50" />
 </product>
 <product name="plum jam">
   <region name="east" sales="320.20" />
   <region name="north" sales="39.50" />
 </product>

</products> File: Transform.xslt <?xml version="1.0"?> <products xsl:version="2.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:for-each select="products/product">
   <xsl:sort select="sum(region/@sales)" order="descending" />
   <product name="{@name}"
     sales="{format-number(sum(region/@sales), "$####0.00")}" />
 </xsl:for-each>

</products> Output: <?xml version="1.0" encoding="UTF-8"?><products><product name="plum jam" sales="$359.70"/><product name="raspberry jam" sales="$215.66"/><product name="strawberry jam" sales="$70.00"/></products>

</source>
   
  


number format="001. "

   <source lang="xml">

File: Data.xml <colors>

 <color>red</color>
 <color>green</color>
 <color>blue</color>
 <color>yellow</color>
 <color>purple</color>
 <color>brown</color>
 <color>orange</color>
 <color>pink</color>
 <color>black</color>
 <color>white</color>
 <color>gray</color>

</colors> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 
 <xsl:template match="color">
   <xsl:number format="001. " />
   <xsl:apply-templates />
 </xsl:template>
 

</xsl:stylesheet> Output:

 001. red
 002. green
 003. blue
 004. yellow
 005. purple
 006. brown
 007. orange
 008. pink
 009. black
 010. white
 011. gray
</source>
   
  


number format="1. " level="any"

   <source lang="xml">

File: Data.xml

<book>

 <title>Title of Book</title>
 <chapter>
   <title>First Chapter</title>
   <sect1>
     <title>First Section, First Chapter</title>
     <figure>
       <title>First picture in book</title>
       <graphic fileref="pic1.jpg" />
     </figure>
   </sect1>
 </chapter>

</book> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 
 <xsl:template match="figure">
   <xsl:number format="1. " level="any" />
   <xsl:apply-templates />
 </xsl:template>

</xsl:stylesheet> Output:

 Title of Book
 
   First Chapter
   
     First Section, First Chapter
     1. 
       First picture in book
       
     
   
 
</source>
   
  


number format="1. " level="any" from="chapter"

   <source lang="xml">

File: Data.xml

<book>

 <title>Title of Book</title>
 <chapter>
   <title>First Chapter</title>
   <sect1>
     <title>First Section, First Chapter</title>
     <figure>
       <title>First picture in book</title>
       <graphic fileref="pic1.jpg" />
     </figure>
   </sect1>
 </chapter>

</book> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 
 <xsl:template match="figure">
   <xsl:number format="1. " level="any" from="chapter" />
   <xsl:apply-templates />
 </xsl:template>

</xsl:stylesheet> Output:

 Title of Book
 
   First Chapter
   
     First Section, First Chapter
     1. 
       First picture in book
       
     
   
 
</source>
   
  


number format="1. " level="multiple"

   <source lang="xml">

File: Data.xml <book>

 <title>Title of Book</title>
 <chapter>
   <title>First Chapter</title>
   <sect1>
     <title>First Section, First Chapter</title>
     <figure>
       <title>First picture in book</title>
       <graphic fileref="pic1.jpg" />
     </figure>
   </sect1>
 </chapter>

</book>

File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 
 <xsl:template match="sect1">
   <xsl:number format="1. " level="multiple" />
   <xsl:apply-templates />
 </xsl:template>
 

</xsl:stylesheet> Output:

 Title of Book
 
   First Chapter
   1. 
     First Section, First Chapter
     
       First picture in book
       
     
   
 
</source>
   
  


number format="1. " level="multiple" count="chapter|sect1"

   <source lang="xml">

File: Data.xml <book>

 <title>Title of Book</title>
 <chapter>
   <title>First Chapter</title>
   <sect1>
     <title>First Section, First Chapter</title>
     <figure>
       <title>First picture in book</title>
       <graphic fileref="pic1.jpg" />
     </figure>
   </sect1>
 </chapter>

</book> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" />
 <xsl:template match="sect1">
   <xsl:number format="1. " level="multiple" count="chapter|sect1" />
   <xsl:apply-templates />
 </xsl:template>

</xsl:stylesheet>

 Title of Book
 
   First Chapter
   1.1. 
     First Section, First Chapter
     
       First picture in book
       
     
   
 
</source>
   
  


number format="1. " level="multiple" count="chapter|sect1|sect2"

   <source lang="xml">

File: Data.xml <book>

 <title>Title of Book</title>
 <chapter>
   <title>First Chapter</title>
   <sect1>
     <title>First Section, First Chapter</title>
     <figure>
       <title>First picture in book</title>
       <graphic fileref="pic1.jpg" />
     </figure>
   </sect1>
 </chapter>

</book> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 <xsl:template match="chapter">
   <xsl:number format="1. " />
   <xsl:apply-templates />
 </xsl:template>
 <xsl:template match="sect1">
   <xsl:number format="1. " level="multiple" count="chapter|sect1" />
   <xsl:apply-templates />
 </xsl:template>
 <xsl:template match="sect2">
   <xsl:number format="1. " level="multiple" count="chapter|sect1|sect2" />
   <xsl:apply-templates />
 </xsl:template>

</xsl:stylesheet> Output:

 Title of Book
 1. 
   First Chapter
   1.1. 
     First Section, First Chapter
     
       First picture in book
       
     
   
 
</source>
   
  


number level="multiple" format="1. "

   <source lang="xml">

File: Data.xml <colors>

 <color>red</color>
 <color>green</color>
 <color>blue</color>
 <color>egg</color>
 <color>navy</color>

</colors> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 
 <xsl:template match="color">
   <xsl:number level="multiple" format="1. " />
   <xsl:apply-templates />
 </xsl:template>
 

</xsl:stylesheet> Output:

 1. red
 2. green
 3. blue
 4. egg
 5. navy
</source>
   
  


Number style

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <book>

 <title>XSLT Topics</title>
 <chapter>
   <title>XPath</title>
   <para>text</para>
 </chapter>
 <chapter>
   <title>Stylesheet Basics</title>
   <para>text</para>
 </chapter>
 <chapter>
   <title>Branching and Control Elements</title>
   <para>text</para>
 </chapter>
 <chapter>
   <title>Functions</title>
   <para>text</para>
 </chapter>

</book> File: Transform.xslt <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:import href="standard-style.xsl" />
 <xsl:template match="line">
   <xsl:number level="any" format="001" />
     
   <xsl:apply-imports />
 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>

 XSLT Topics
 
   XPath
   text
 
 
   Stylesheet Basics
   text
 
 
   Branching and Control Elements
   text
 
 
   Functions
   text
 
</source>
   
  


Various number formats

   <source lang="xml">

File: Data.xml <colors>

 <color>red</color>
 <color>green</color>
 <color>blue</color>
 <color>yellow</color>

</colors> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 
 <xsl:template match="colors">
   <xsl:for-each select="color">
     <xsl:number format="I. " />
     <xsl:value-of select="." />
     <xsl:text/>
   </xsl:for-each>
   <xsl:text>|</xsl:text>
   <xsl:for-each select="color">
     <xsl:number format="i. " />
     <xsl:value-of select="." />
     <xsl:text/>
   </xsl:for-each>
   <xsl:text>|</xsl:text>
   <xsl:for-each select="color">
     <xsl:number format="A. " />
     <xsl:value-of select="." />
     <xsl:text/>
   </xsl:for-each>
   <xsl:text>|</xsl:text>
   <xsl:for-each select="color">
     <xsl:number format="a. " />
     <xsl:value-of select="." />
     <xsl:text/>
   </xsl:for-each>
 </xsl:template>
 

</xsl:stylesheet> Output: I. redII. greenIII. blueIV. yellow|i. redii. greeniii. blueiv. yellow|A. redB. greenC. blueD. yellow|a. redb. greenc. blued. yellow

</source>