Output Number from any level : number « XSLT stylesheet « XML






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>

 








Related examples in the same category

1.Add number list to the output
2.Output number and count
3.number column
4.Illustrates use of xsl:number element
5.number count="name" format=" 1. "
6.number level="multiple" count="*" from="data" format="1.1.1 "
7.number value="1000000" grouping-size="3" grouping-separator="."
8.format number by locale
9.number from="section" level="multiple" count="section | item" format=" a "
10.number format=" " grouping-size="3" grouping-separator=","
11.number format=" 1. "
12.number level="multiple" count="section | item" format="i. a. "
13.number count="manufacturer|car" level="multiple" format="1.1. "
14.number count="manufacturer" level="multiple" format="1."
15.number count="car" level="single" format="w"
16.number level="multiple" count="chapter|sect1|sect2|sect3" format="1.1.1.1. "
17.number level="any" count="chapter|sect1|sect2|sect3" format="1. "
18.number level="single" count="chapter|sect1|sect2|sect3" format="1.1.1.1. "
19.number level="multiple" count="chapter|sect1|sect2" format="I-A-i: "
20.number level="any" from="book" count="chapter" format="1."