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
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

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.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

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


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