WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 4 (3/4)
[previous] [next] |
Inside XSLT
Listing 2.5: planets.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="https://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>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="https://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.
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.
[previous] [next] |
Created: October 4, 2001
Revised: October 4, 2001
URL: https://webreference.com/authoring/languages/xml/insidexslt/chap2/4/3.html