Illustrates use of xsl:number element : number « XSLT stylesheet « XML






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>

 








Related examples in the same category

1.Add number list to the output
2.Output number and count
3.Output Number from any level
4.number column
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."