While this is a good comparison to help illustrate the difference between
2.3 Another XSLT Example, Using XHTML
Example 2-5 contains XML data from an imaginary scheduling program. A schedule has an owner followed by a list of appointments. Each appointment has a date, start time, end time, subject, location, and optional notes. Needless to say, a true scheduling application probably has a lot more data, such as repeating appointments, alarms, categories, and many other bells and whistles. Assuming that the scheduler stores its data in XML files, we can easily add features later by writing a stylesheet to convert the existing XML files to some new format.
Example 2-5. schedule.xml
<?xml version="1.0" encoding="UTF -8"?>
<?xml-stylesheet type="text/xsl" href="schedule.xslt"?>
<schedule>
<owner>
<name>
<first>Eric</first>
<last>Burke</last>
</name>
</owner>
<appointment>
<when>
<date month="03" day="15" year="2001"/>
<startTime hour="09" minute="30 "/>
<endTime hour="10" minute="30"/>
</when>
<subject>Interview potential new hire</subject>
<location>Rm 103</location>
<note>Ask Bob for an updated resume.</note>
</appointment>
<appointment>
<when>
<date month="03" day="15" year="2001"/>
<startTime hour="15" minute="30"/>
<endTime hour="16" minute="30"/>
</when>
<subject>Dr. Appointment</subject>
<location>1532 Main Street</location>
</appointment>
<appointment>
<when>
<date month="03" day="16" year="2001"/>
<startTime hour="11" minute="30"/>
<endTime hour="12" minute="30"/>
</when>
<subject>Lunch w/Boss</subject>
<location>Pizza Place on First Capitol Drive</location>
</appointment>
</schedule>
As you can see, the XML document uses both attributes (month="03") and child elements to represent its data. XSLT has the ability to search for and transform both types of data, as well as comments, processing instructions, and text. In our current document, the appointments are stored in chronological order. Later, we will see how to change the sort order using <xsl:sort>.
Unlike the earlier example, the second line of Example 2-5 contains a reference to the XSLT stylesheet:
<?xml-stylesheet type="text/xsl" href="schedule.xslt"?>
This processing instruction is entirely optional. When viewing the XML document in a web browser that supports XSLT, this is the stylesheet that is used. If you apply the stylesheet from the command line or from a server-side process, however, you normally specify both the XML document and the XSLT document as parameters to the processor. Because of this capability, the processing instruction shown does not force that particular stylesheet to be used. From a development perspective, including this line quickly displays your work because you simply load the XML document into a compatible web browser, and the stylesheet is loaded automatically.
In this book, the xml-stylesheet processing instruction uses type="text/xsl". However, some processors use type="text/xml", which does not work with Microsoft Internet Explorer. The XSLT specification contains one example, which uses "text/xml".
Figure 2-3 shows the XHTML output from an XSLT transformation of schedule.xml. As you can see, the stylesheet is capable of producing content that does not appear in the original XML data, such as "Subject:". It can also selectively copy element content and attribute values from the XML source to the result tree; nothing requires every piece of data to be copied.
Figure 2-3. XHTML output
The XSLT stylesheet that produces this output is shown in Example 2-6. As mentioned
previously, XSLT stylesheets must be well-formed XML documents. Once again, we use .xslt as the filename extension, but .xsl is also common. This stylesheet is based on the skeleton document presented in Example 2-4. However, it produces XHTML instead of HTML.
Example 2-6. schedule.xslt
<?xml version="1.0" encoding="UTF -8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctypesystem="http://www.w3.org/TR/xhtml1/DTD/xhtml1 -transitional.dtd"/>
<!--********************************** ******************************
** "/" template
************************************************************* -->
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Schedule</title>
</head>
<body>
<h2 align="center">
<xsl:value-of select="schedule/owner/name/first"/>
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
<xsl:value-of select="schedule/owner/name/last"/>'s Schedule</h2>
<xsl:apply-templates select="schedule/appointment"/>
</body>
<xsl:template match="appointment">
<hr/>
<h3>Appointment</h3>
<xsl:apply-templates select="when"/>
<table>
</td>
</tr>
</table>
</xsl:template>
<!--****************************************************************
** "when" template
************************************************************* -->
<xsl:template match="when">
<p>
<xsl:value-of select="date/@month"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="date/@day"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="date/@year"/>
from
<xsl:value-of select="startTime/@hour"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="startTime/@minute"/>
until
<xsl:value-of select="endTime/@hour"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="endTime/@minute"/>
</p>
</xsl:template>
</xsl:stylesheet>
The first part of this stylesheet should look familiar. The first four lines are typical of just about any stylesheet you will write. Next, the output method is specified as xml because this stylesheet is producing XHTML instead of HTML:
<xsl:output method="xml"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctypesystem="http://www.w3.org/TR/xhtml1/DTD/xhtml1 -transitional.dtd"/>
The <xsl:output> element produces the following XHTML content:
<?xml version="1.0" encoding="UTF -16"?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1 -transitional.dtd">
Moving on, the first template in the stylesheet matches "/" and outputs the skeleton for the XHTML document. Another requirement for XHTML is the namespace attribute on the <html>
element:
<html xmlns="http://www.w3.org/1999/xhtml">
The remainder of schedule.xslt consists of additional templates, each of which matches a particular pattern in the XML input.