spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / authoring / languages / xml / insidexslt / chap2 / 4 To page 1To page 2current pageTo page 4
[previous] [next]

Inside XSLT

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

Listing 2.5: planets.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/PLANETS">
    <HTML>
      <HEAD>
        <TITLE>
          The Planets Table
        </TITLE>
      </HEAD>
      <BODY>
        <H1>
          The Planets Table
        </H1>
        <TABLE BORDER="2">
          <TR>
            <TD>Name</TD>
            <TD>Mass</TD>
            <TD>Radius</TD>
            <TD>Day</TD>
          </TR>
          <xsl:apply-templates/>
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>

  <xsl:template match="PLANET">
    <TR>
      <TD><xsl:value-of select="NAME"/></TD>
      <TD><xsl:value-of select="MASS"/></TD>
      <TD><xsl:value-of select="RADIUS"/></TD>
      <TD><xsl:value-of select="DAY"/></TD>
    </TR>
  </xsl:template>
</xsl:stylesheet>

That's all you need; here's the result:

<HTML>
  <HEAD>
    <TITLE>
      The Planets Table
    </TITLE>
  </HEAD>
  <BODY>
    <H1>
      The Planets Table
    </H1>
    <TABLE BORDER="2">
      <TR>
        <TD>Name</TD>
        <TD>Mass</TD>
        <TD>Radius</TD>
        <TD>Day</TD>
      </TR>
      <TR>
        <TD>Mercury</TD>
        <TD>.0553</TD>
        <TD>1516</TD>
        <TD>58.65</TD>
      </TR>
      <TR>
        <TD>Venus</TD>
        <TD>.815</TD>
        <TD>3716</TD>
        <TD>116.75</TD>
      </TR>
      <TR>
        <TD>Earth</TD>
        <TD>1</TD>
        <TD>2107</TD>
        <TD>1</TD>
      </TR>
    </TABLE>
  </BODY>
</HTML>

This is almost what we want. If you look at Figure 2.2, you'll see that this HTML file doesn't list the value of the UNITS attribute that each element (except the <NAME> attribute) has in planets.xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xml" href="planets.xsl"?>
<PLANETS>

  <PLANET>
    <NAME>Mercury</NAME>
    <MASS UNITS="(Earth = 1)">.0553</MASS>
    <DAY UNITS="days">58.65</DAY>
    <RADIUS UNITS="miles">1516</RADIUS>
    <DENSITY UNITS="(Earth = 1)">.983</DENSITY>
    <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->
  </PLANET>
        .
        .
        .

Chapter 3, which works with templates in more detail, shows how to extract the value of the attributes from XML elements.

Figure 2.2. Planets.html without attributes 
in the Internet Explorer
Figure 2.2 Planets.html without attributes in the Internet Explorer.

In the meantime, before you start working with templates in detail, you need to understand a lot more about stylesheets in general. For example, the XSLT 1.1 working draft included support for the XML Base recommendation, which means it will also appear in XSLT 2.0.


home / authoring / languages / xml / insidexslt / chap2 / 4 To page 1To page 2current pageTo page 4
[previous] [next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
Working with the DOM Stylesheets Collection · Administering RBAC in PHP 5 CMS Framework · xref: Automatic Cross Referencing Script
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Combine BottomCount() with Other MDX Functions to Add Sophistication · Creating a Daemon with Python · The Coming Voice-over-WiMAX Revolution

Created: October 4, 2001
Revised: October 4, 2001


URL: http://webreference.com/authoring/languages/xml/insidexslt/chap2/4/3.html