Table cell format : table « XSLT stylesheet « XML Tutorial






File: Data.xml
<?xml version="1.0" ?>
<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>
        <FONT SIZE="6">
          <B>Student Transcript</B>
        </FONT>
        <P />

        <xsl:apply-templates select="student" />

        <HR />
        <TABLE ALIGN="left" BORDER="1" CELLPADDING="4">
          <TR>
            <td>Course Name</TH>
            <td>Grade</TH>
            <TH ALIGN="right">Credits</TH>
          </TR>
          <xsl:apply-templates select="term" />
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>

  <xsl:template match="student">
    <FONT SIZE="4">
      <B>
        Name:
        <I>
          <xsl:value-of select="@name" />
        </I>
        <BR />
        ID:
        <I>
          <xsl:value-of select="@number" />
        </I>
      </B>
    </FONT>
    <P />
  </xsl:template>

  <xsl:template match="term">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="heading">
    <TR>
      <TH COLSPAN="3">
        <xsl:value-of select="@name" />
      </TH>
    </TR>
  </xsl:template>

  <xsl:template match="course">
    <TR>
      <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><Paragraph></Paragraph><FONT SIZE="4"><B>
                    Name:
                    <I>name 1</I><BR>
                    ID:
                    <I></I></B></FONT><Paragraph></Paragraph>
      <HR>
      <TABLE ALIGN="left" BORDER="1" CELLPADDING="4">
         <TR>
            <Td>Course Name</Td>
            <Td>Grade</Td>
            <Td ALIGN="right">Credits</Td>
         </TR>
             
         <TR>
            <Td COLSPAN="3">Winter 1999</Td>
         </TR>
             
         <TR>
            <TD>course 1</TD>
            <TD>A-</TD>
            <TD ALIGN="right">4</TD>
         </TR>
             
         <TR>
            <TD>course 2</TD>
            <TD>B+</TD>
            <TD ALIGN="right">3</TD>
         </TR>
           
      </TABLE>
   </BODY>
</HTML>








5.5.table
5.5.1.Use for-each loop to output table row
5.5.2.Table cell format
5.5.3.Format table cell with choose statement
5.5.4.Set table cell style with choose statement
5.5.5.generates a table with selected elements,with the number of elements per row given in the stylesheet