XML/XSLT stylesheet/number

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

Add number list to the output

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="color">
    <xsl:number />
    .
    <xsl:apply-templates />
  </xsl:template>
  
</xsl:stylesheet>
Output:

  1
    .
    red
  2
    .
    green
  3
    .
    blue
  4
    .
    yellow



format number by locale

File: Data.xml
<?xml version="1.0"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<format>
 <number>100</number>
 <number>1000</number>
 <number>10000</number>
 <number>100000</number>
 <number>1000000</number>
 <number>10000000</number>
 <number>100000000</number>
 <number>1000000000</number>
</format>

File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" />
  <xsl:decimal-format name="de" decimal-separator=","
    grouping-separator="." />
  <xsl:decimal-format name="fr" decimal-separator=","
    grouping-separator=" " />
  <xsl:decimal-format name="ru" decimal-separator=","
    grouping-separator=" " />
  <xsl:decimal-format name="uk" decimal-separator="."
    grouping-separator="," />
  <xsl:decimal-format name="us" decimal-separator="."
    grouping-separator="," />
  <xsl:template match="format">
    <html>
      <head>
        <title>Number Formatter</title>
        <style type="text/css">
          table {margin-left:auto;margin-right:auto} td
          {text-align:right;padding: 5px 5px 5px 5px} h3
          {text-align:center}
        </style>
      </head>
      <body>
        <h3>Number Formatter</h3>
        <table rules="all">
          <thead>
            <tr>
              <th>Deutschland</th>
              <th>France</th>
              <th>
                &#x420;&#x43E;&#x441;&#x441;&#x438;&#x44f;
              </th>
              <th>United Kingdom</th>
              <th>United States</th>
            </tr>
          </thead>
          <tbody>
            <xsl:apply-templates select="number" />
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="number">
    <tr>
      <td>
        <xsl:value-of
          select="format-number(.,".###,00&#x20AC;","de")" />
      </td>
      <td>
        <xsl:value-of
          select="format-number(.," ###,00&#x20AC;","fr")" />
      </td>
      <td>
        <xsl:value-of
          select="format-number(.," ###,00p.","ru")" />
      </td>
      <td>
        <xsl:value-of
          select="format-number(.,"&#xA3;,###.00","uk")" />
      </td>
      <td>
        <xsl:value-of
          select="format-number(.,"&#x24;,###.00","us")" />
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Number Formatter</title><style type="text/css">
          table {margin-left:auto;margin-right:auto} td
          {text-align:right;padding: 5px 5px 5px 5px} h3
          {text-align:center}
        </style></head>
   <body>
      <h3>Number Formatter</h3>
      <table rules="all">
         <thead>
            <tr>
               <th>Deutschland</th>
               <th>France</th>
               <th>
                                  ??????
                                
               </th>
               <th>United Kingdom</th>
               <th>United States</th>
            </tr>
         </thead>
         <tbody>
            <tr>
               <td>100,00?td>
               <td>100,00?td>
               <td>100,00p.</td>
               <td>?0.00</td>
               <td>$100.00</td>
            </tr>
            <tr>
               <td>1.000,00?td>
               <td>1 000,00?td>
               <td>1 000,00p.</td>
               <td>?000.00</td>
               <td>$1,000.00</td>
            </tr>
            <tr>
               <td>10.000,00?td>
               <td>10 000,00?td>
               <td>10 000,00p.</td>
               <td>?,000.00</td>
               <td>$10,000.00</td>
            </tr>
            <tr>
               <td>100.000,00?td>
               <td>100 000,00?td>
               <td>100 000,00p.</td>
               <td>?0,000.00</td>
               <td>$100,000.00</td>
            </tr>
            <tr>
               <td>1.000.000,00?td>
               <td>1 000 000,00?td>
               <td>1 000 000,00p.</td>
               <td>?000,000.00</td>
               <td>$1,000,000.00</td>
            </tr>
            <tr>
               <td>10.000.000,00?td>
               <td>10 000 000,00?td>
               <td>10 000 000,00p.</td>
               <td>?,000,000.00</td>
               <td>$10,000,000.00</td>
            </tr>
            <tr>
               <td>100.000.000,00?td>
               <td>100 000 000,00?td>
               <td>100 000 000,00p.</td>
               <td>?0,000,000.00</td>
               <td>$100,000,000.00</td>
            </tr>
            <tr>
               <td>1.000.000.000,00?td>
               <td>1 000 000 000,00?td>
               <td>1 000 000 000,00p.</td>
               <td>?000,000,000.00</td>
               <td>$1,000,000,000.00</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>



Illustrates use of xsl:number element

File: Data.xml
<?xml version="1.0" standalone="no" ?>
<transcript>
  <student id="STU12345" name="name 1" status="active">
    <home_address>35 Wall Street, Wonderland, NJ</home_address>
    <interests>
      <interest>interest 1</interest>
      <interest>interest 2</interest>
      <interest>interest 3</interest>
    </interests>
  </student>
  <term>
    <heading name="Winter 1999" />
    <course>
      <course-name>course 1</course-name>
      <grade>A-</grade>
      <credits>4</credits>
    </course>
    <course>
      <course-name>course 2</course-name>
      <grade>B+</grade>
      <credits>3</credits>
    </course>
  </term>
  <summary>summary</summary>
  <comments>
     
    comments
  </comments>
</transcript>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="transcript">
    <HTML>
      <BODY>
        <xsl:apply-templates select="student" />
        <HR />
        <TABLE BORDER="1" CELLPADDING="4">
          <TR ALIGN="left">
            <TH ALIGN="right">#</TH>
            <TH>Course Name</TH>
            <TH>Grade</TH>
            <TH ALIGN="right">Credits</TH>
          </TR>
          <xsl:apply-templates select="term/course">
            <xsl:sort select="course-name" />
          </xsl:apply-templates>
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>
  <xsl:template match="student">
    <FONT SIZE="6">
      <B>Student Transcript</B>
    </FONT>
    <P />
    <FONT SIZE="4">
      <B>
        Name:
        <I>
          <xsl:value-of select="@name" />
        </I>
        <BR />
        ID:
        <I>
          <xsl:value-of select="@id" />
        </I>
      </B>
    </FONT>
    <P />
  </xsl:template>
  <xsl:template match="course">
    <TR>
      <TD ALIGN="right">
        <xsl:number value="position()" />
        <xsl:text> of </xsl:text>
        <xsl:number value="last()" />
      </TD>
      <TD>
        <xsl:value-of select="course-name" />
      </TD>
      <TD>
        <xsl:value-of select="grade" />
      </TD>
      <TD ALIGN="right">
        <xsl:value-of select="credits" />
      </TD>
    </TR>
  </xsl:template>
</xsl:stylesheet>
Output:
<HTML>
   <BODY><FONT SIZE="6"><B>Student Transcript</B></FONT><P></P><FONT SIZE="4"><B>
                    Name:
                    <I>name 1</I><BR>
                    ID:
                    <I>STU12345</I></B></FONT><P></P>
      <HR>
      <TABLE BORDER="1" CELLPADDING="4">
         <TR ALIGN="left">
            <TH ALIGN="right">#</TH>
            <TH>Course Name</TH>
            <TH>Grade</TH>
            <TH ALIGN="right">Credits</TH>
         </TR>
         <TR>
            <TD ALIGN="right">1 of 2</TD>
            <TD>course 1</TD>
            <TD>A-</TD>
            <TD ALIGN="right">4</TD>
         </TR>
         <TR>
            <TD ALIGN="right">2 of 2</TD>
            <TD>course 2</TD>
            <TD>B+</TD>
            <TD ALIGN="right">3</TD>
         </TR>
      </TABLE>
   </BODY>
</HTML>



number count="car" level="single" format="w"

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- cars.xml -->
<cars>
  <manufacturer name="Chevrolet">
    <car>Cavalier</car>
    <car>Corvette</car>
    <car>Impala</car>
    <car>Malibu</car>
  </manufacturer>
  <manufacturer name="Ford">
    <car>Pinto</car>
    <car>Mustang</car>
    <car>Taurus</car>
  </manufacturer>
  <manufacturer name="Volkswagen">
    <car>Beetle</car>
    <car>Jetta</car>
    <car>Passat</car>
    <car>Touraeg</car>
  </manufacturer>
</cars>
File: Transform.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:text>Automobile manufacturers and their cars&#xA;</xsl:text>
    <xsl:for-each select="cars/manufacturer">
      <xsl:value-of select="@name"/>
      <xsl:text>&#xA;</xsl:text>
      <xsl:for-each select="car">
        <xsl:text>  Car </xsl:text>
        <xsl:number count="car" level="single" format="w"/>
        <xsl:text>: </xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
Automobile manufacturers and their cars
Chevrolet
  Car one: Cavalier
  Car two: Corvette
  Car three: Impala
  Car four: Malibu
Ford
  Car one: Pinto
  Car two: Mustang
  Car three: Taurus
Volkswagen
  Car one: Beetle
  Car two: Jetta
  Car three: Passat
  Car four: Touraeg



number count="manufacturer|car" level="multiple" format="1.1. "

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<cars>
  <manufacturer name="Chevrolet">
    <car>Cavalier</car>
    <car>Corvette</car>
    <car>Impala</car>
    <car>Malibu</car>
  </manufacturer>
  <manufacturer name="Ford">
    <car>Pinto</car>
    <car>Mustang</car>
    <car>Taurus</car>
  </manufacturer>
  <manufacturer name="Volkswagen">
    <car>Beetle</car>
    <car>Jetta</car>
    <car>Passat</car>
    <car>Touraeg</car>
  </manufacturer>
</cars>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:text>Automobile manufacturers and their cars&#xA;</xsl:text>
    <xsl:for-each select="cars/manufacturer">
      <xsl:value-of select="@name"/>
      <xsl:text>&#xA;</xsl:text>
      <xsl:for-each select="car">
        <xsl:sort select="."/>
        <xsl:text>  </xsl:text>
        <xsl:if test="(position() mod 2) = 0">
          <xsl:number count="manufacturer|car" level="multiple" format="1.1. "/>
        </xsl:if>
        <xsl:value-of select="."/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
Automobile manufacturers and their cars
Chevrolet
  Cavalier
  1.2. Corvette
  Impala
  1.4. Malibu
Ford
  Mustang
  2.1. Pinto
  Taurus
Volkswagen
  Beetle
  3.2. Jetta
  Passat
  3.4. Touraeg



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

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<cars>
  <manufacturer name="Chevrolet">
    <car>Cavalier</car>
    <car>Corvette</car>
    <car>Impala</car>
    <car>Malibu</car>
  </manufacturer>
  <manufacturer name="Ford">
    <car>Pinto</car>
    <car>Mustang</car>
    <car>Taurus</car>
  </manufacturer>
  <manufacturer name="Volkswagen">
    <car>Beetle</car>
    <car>Jetta</car>
    <car>Passat</car>
    <car>Touraeg</car>
  </manufacturer>
</cars>

File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:text>Automobile manufacturers and their cars&#xA;</xsl:text>
    <xsl:for-each select="cars/manufacturer">
      <xsl:value-of select="@name"/>
      <xsl:text>&#xA;</xsl:text>
      <xsl:for-each select="car">
        <xsl:sort select="."/>
        <xsl:text>  </xsl:text>
        <xsl:if test="(position() mod 2) = 0">
          <xsl:number count="manufacturer" level="multiple" format="1."/>
          <xsl:value-of select="position()"/>
          <xsl:text>. </xsl:text>
        </xsl:if>
        <xsl:value-of select="."/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
Automobile manufacturers and their cars
Chevrolet
  Cavalier
  1.2. Corvette
  Impala
  1.4. Malibu
Ford
  Mustang
  2.2. Pinto
  Taurus
Volkswagen
  Beetle
  3.2. Jetta
  Passat
  3.4. Touraeg



number count="name" format=" 1. "

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<provinces>
 <name>Alberta</name>
 <name>British Columbia</name>
 <name>Manitoba</name>
 <name>New Brunswick</name>
 <name>Newfoundland and Labrador</name>
 <name>Northwest Territories</name>
 <name>Nova Scotia</name>
 <name>Nunavut</name>
 <name>Ontario</name>
 <name>Prince Edward Island</name>
 <name>Quebec</name>
 <name>Saskatchewan</name>
 <name>Yukon</name>
</provinces>
File: Transform.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="provinces">
    <xsl:apply-templates select="name" />
  </xsl:template>
  <xsl:template match="name">
    <xsl:number count="name" format=" 1. " />
    <xsl:value-of select="." />
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>
 1. Alberta
 2. British Columbia
 3. Manitoba
 4. New Brunswick
 5. Newfoundland and Labrador
 6. Northwest Territories
 7. Nova Scotia
 8. Nunavut
 9. Ontario
 10. Prince Edward Island
 11. Quebec
 12. Saskatchewan
 13. Yukon



number format=" 1. "

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<provinces>
 <name>Alberta</name>
 <name>British Columbia</name>
 <name>Manitoba</name>
 <name>New Brunswick</name>
 <name>Newfoundland and Labrador</name>
 <name>Northwest Territories</name>
 <name>Nova Scotia</name>
 <name>Nunavut</name>
 <name>Ontario</name>
 <name>Prince Edward Island</name>
 <name>Quebec</name>
 <name>Saskatchewan</name>
 <name>Yukon</name>
</provinces>

File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="provinces">
    <xsl:apply-templates select="name" />
  </xsl:template>
  <xsl:template match="name">
    <xsl:number format=" 1. " />
    <xsl:value-of select="." />
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
 1. Alberta
 2. British Columbia
 3. Manitoba
 4. New Brunswick
 5. Newfoundland and Labrador
 6. Northwest Territories
 7. Nova Scotia
 8. Nunavut
 9. Ontario
 10. Prince Edward Island
 11. Quebec
 12. Saskatchewan
 13. Yukon



number format=" " grouping-size="3" grouping-separator=","

File: Data.xml
<generator>
 <num/>
 <num/>
 <num/>
</generator>

File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="generator">
    <xsl:apply-templates select="num" />
  </xsl:template>
  <xsl:template match="num">
    <xsl:number format="&#10;" grouping-size="3"
      grouping-separator="," />
  </xsl:template>
</xsl:stylesheet>
Output:

1
2
3



number from="section" level="multiple" count="section | item" format=" a "

File: Data.xml
<?xml version="1.0"?>
<outline>
 <section title="US coin denominations">
  <item>cent</item>
  <item>nickel</item>
  <item>dime</item>
  <item>quarter</item>
  <item>half dollar</item>
  <item>dollar</item>
 </section>
</outline>

File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="outline">
    <xsl:for-each select="section|//item">
      <xsl:number from="section" level="multiple"
        count="section | item" format=" a " />
      <xsl:value-of select="@title | text()" />
      <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
    <xsl:text>&#10; </xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
 a US coin denominations
 a cent
 b nickel
 c dime
 d quarter
 e half dollar
 f dollar



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

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>
  <chapter>
    <title>Creating Links and Cross-References</title>
    <para>text</para>
  </chapter>
  <chapter>
    <title>Sorting and Grouping Elements</title>
    <para>text</para>
  </chapter>
  <chapter>
    <title>Combining XML Documents</title>
    <para>text</para>
  </chapter>
</book>
File: Transform.xslt
<?xml version="1.0"?>
<!-- number2.xsl -->
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="book">
    <xsl:for-each select="chapter|.//sect1|.//sect2|.//sect3">
      <xsl:number level="any" count="chapter|sect1|sect2|sect3" format="1. "/>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
1. XPath
2. Stylesheet Basics
3. Branching and Control Elements
4. Functions
5. Creating Links and Cross-References
6. Sorting and Grouping Elements
7. Combining XML Documents



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

File: Data.xml
<?xml version="1.0"?>
<book>
  <title>XSLT Topics</title>
  <chapter>
    <title>title 1</title>
  </chapter>
  <chapter>
    <title>title 2</title>
  </chapter>
  <chapter>
    <title>title 3</title>
  </chapter>
  <chapter>
    <title>title 4</title>
  </chapter>
  <chapter>
    <title>title 5</title>
  </chapter>
  <chapter>
    <title>title 6</title>
  </chapter>
  <chapter>
    <title>title 7</title>
  </chapter>
</book>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="book">
    <xsl:for-each select=".//sect3">
      <xsl:number level="any" from="book" count="chapter" format="1."/>
      <xsl:number level="any" from="chapter" count="sect1" format="1."/>
      <xsl:number level="any" from="sect1" count="sect2" format="1."/>
      <xsl:number level="any" from="sect2" count="sect3" format="1. "/>
      <xsl:value-of select="title"/>
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>



number level="multiple" count="chapter|sect1|sect2" format="I-A-i: "

File: Data.xml
<?xml version="1.0"?>
<book>
  <title>XSLT Topics</title>
  <chapter>
    <title>title 1</title>
  </chapter>
  <chapter>
    <title>title 2</title>
  </chapter>
  <chapter>
    <title>title 3</title>
  </chapter>
  <chapter>
    <title>title 4</title>
  </chapter>
  <chapter>
    <title>title 5</title>
  </chapter>
  <chapter>
    <title>title 6</title>
  </chapter>
  <chapter>
    <title>title 7</title>
  </chapter>
</book>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="book">
    <xsl:for-each select=".">
      <xsl:number level="multiple" count="chapter|sect1|sect2" format="I-A-i: "/>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>



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

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>
  <chapter>
    <title>Creating Links and Cross-References</title>
    <para>text</para>
  </chapter>
  <chapter>
    <title>Sorting and Grouping Elements</title>
    <para>text</para>
  </chapter>
  <chapter>
    <title>Combining XML Documents</title>
    <para>text</para>
  </chapter>
</book>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="book">
    <xsl:for-each select="chapter|.//sect1|.//sect2|.//sect3">
      <xsl:number level="multiple" count="chapter|sect1|sect2|sect3" format="1.1.1.1. "/>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
1. XPath
2. Stylesheet Basics
3. Branching and Control Elements
4. Functions
5. Creating Links and Cross-References
6. Sorting and Grouping Elements
7. Combining XML Documents



number level="multiple" count="*" from="data" format="1.1.1 "

File: Data.xml
<?xml version="1.0" encoding="US-ASCII"?>
<data locale="us">
  <record>
    <name>
      <full>A</full>
      <brief>I</brief>
    </name>
    <address>
      <street>uite 330</street>
      <city>Regina</city>
      <state>SK</state>
      <code>90292</code>
      <nation>USA</nation>
    </address>
    <tel>
      <phone>+1 310 823 9358</phone>
      <fax>+1 310 823 8649</fax>
      <email>i@i.org</email>
    </tel>
  </record>
</data>

File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:strip-space elements="*" />
  <xsl:template match="/">
    <xsl:apply-templates select="data//*" />
  </xsl:template>
  <xsl:template match="data//*">
    <xsl:number level="multiple" count="*" from="data"
      format="1.1.1 " />
    <xsl:value-of select="name()" />
    <xsl:text>: </xsl:text>
    <xsl:text>&#9;</xsl:text>
    <xsl:value-of select="text()" />
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
1 record:   
1.1 name:   
1.1.1 full:   A
1.1.2 brief:   I
1.2 address:   
1.2.1 street:   uite 330
1.2.2 city:   Regina
1.2.3 state:   SK
1.2.4 code:   90292
1.2.5 nation:   USA
1.3 tel:   
1.3.1 phone:   +1 310 823 9358
1.3.2 fax:   +1 310 823 8649
1.3.3 email:   i@i.org



number level="multiple" count="section | item" format="i. a. "

File: Data.xml
<?xml version="1.0"?>
<outline>
 <section title="US coin denominations">
  <item>cent</item>
  <item>nickel</item>
  <item>dime</item>
  <item>quarter</item>
  <item>half dollar</item>
  <item>dollar</item>
 </section>
</outline>

File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="outline">
    <xsl:for-each select="section|//item">
      <xsl:number level="multiple" count="section | item"
        format="i. a. " />
      <xsl:value-of select="@title | text()" />
      <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
    <xsl:text>&#10; </xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
i. US coin denominations
i. a. cent
i. b. nickel
i. c. dime
i. d. quarter
i. e. half dollar
i. f. dollar



number level="single" count="chapter|sect1|sect2|sect3" format="1.1.1.1. "

File: Data.xml
<?xml version="1.0"?>
<book>
  <title>XSLT Topics</title>
  <chapter>
    <title>title 1</title>
  </chapter>
  <chapter>
    <title>title 2</title>
  </chapter>
  <chapter>
    <title>title 3</title>
  </chapter>
  <chapter>
    <title>title 4</title>
  </chapter>
  <chapter>
    <title>title 5</title>
  </chapter>
  <chapter>
    <title>title 6</title>
  </chapter>
  <chapter>
    <title>title 7</title>
  </chapter>
</book>

File: Transform.xslt

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="book">
    <xsl:for-each select="chapter|.//sect1|.//sect2|.//sect3">
      <xsl:number level="single" count="chapter|sect1|sect2|sect3" format="1.1.1.1. "/>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
1. title 1
2. title 2
3. title 3
4. title 4
5. title 5
6. title 6
7. title 7



number value="1000000" grouping-size="3" grouping-separator="."

File: Data.xml
<thanks>Thanks</thanks>

File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<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>
Output:
Thanks1.000.000!



Output number and count

File: Data.xml 
<?xml version="1.0"?>
<shopping-list>
   <item>bananas</item>
   <item>apples</item>
   <special-item>item special</special-item>
   <item>grapes</item>
   <special-item>item 2</special-item>
   <item>cherries</item>
</shopping-list>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="item | special-item">
    <xsl:number count="item | special-item" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="." />
    <br />
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
   1 bananas<br/>
   2 apples<br/>
   3 item special<br/>
   4 grapes<br/>
   5 item 2<br/>
   6 cherries<br/>



Output Number from any level

File: Data.xml 
<poem>
   <author>author 1</author>
   <date>1912</date>
   <title>Song</title>
  <stanza>
      <line>line 1</line>
      <line>line 2</line>
      <line>line 3</line>
      <line>line 4</line>
   </stanza>
   <stanza>
      <line>line 5</line>
      <line>line 6</line>
      <line>line 7</line>
      <line>line 8</line>
   </stanza>
   <stanza>
      <line>line 9</line>
      <line>line 10</line>
      <line>line 11</line>
      <line>line 12</line>
   </stanza>
</poem>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
        <p>
          <xsl:apply-templates select="/poem/stanza" />
        </p>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="stanza">
    <p>
      <table>
        <xsl:apply-templates />
      </table>
    </p>
  </xsl:template>
  <xsl:template match="line">
    <tr>
      <td width="350">
        <xsl:value-of select="." />
      </td>
      <td width="50">
        <xsl:variable name="line-nr">
          <xsl:number level="any" from="poem" />
        </xsl:variable>
        <xsl:if test="$line-nr mod 3 = 0">
          <xsl:value-of select="$line-nr" />
        </xsl:if>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <body>
      <p>
         <p>
            <table>
               
               <tr>
                  <td width="350">line 1</td>
                  <td width="50"></td>
               </tr>
               
               <tr>
                  <td width="350">line 2</td>
                  <td width="50"></td>
               </tr>
               
               <tr>
                  <td width="350">line 3</td>
                  <td width="50">3</td>
               </tr>
               
               <tr>
                  <td width="350">line 4</td>
                  <td width="50"></td>
               </tr>
               
            </table>
         </p>
         <p>
            <table>
               
               <tr>
                  <td width="350">line 5</td>
                  <td width="50"></td>
               </tr>
               
               <tr>
                  <td width="350">line 6</td>
                  <td width="50">6</td>
               </tr>
               
               <tr>
                  <td width="350">line 7</td>
                  <td width="50"></td>
               </tr>
               
               <tr>
                  <td width="350">line 8</td>
                  <td width="50"></td>
               </tr>
               
            </table>
         </p>
         <p>
            <table>
               
               <tr>
                  <td width="350">line 9</td>
                  <td width="50">9</td>
               </tr>
               
               <tr>
                  <td width="350">line 10</td>
                  <td width="50"></td>
               </tr>
               
               <tr>
                  <td width="350">line 11</td>
                  <td width="50"></td>
               </tr>
               
               <tr>
                  <td width="350">line 12</td>
                  <td width="50">12</td>
               </tr>
               
            </table>
         </p>
      </p>
   </body>
</html>