Main Page

XSLT style sheet

XSLT files are called style sheets and are made up of a number of
templates
. A template pertains to a spe-
cific part of an XML file (using XPath) and determines what text is output for that section. By defining
templates for various elements and conditions, and XSLT style sheet becomes a sort of XML parser. For
example, consider the XML used earlier:
<?xml version=”1.0”?>
<employees>
<employee title=”Software Engineer”>
<name>Nicholas C. Zakas</name>
</employee>
<employee title=”Salesperson”>
<name>Jim Smith</name>
</employee>
</employees>
Now suppose you’d like to display the list of employees in the following HTML format:
<html>
<head>
<title>Employees</title>
</head>
<body>
<ul>
<li>Nicholas C. Zakas, <em>Software Engineer</em></li>
<li>Jim Smith, <em>Salesperson</em></li>
</ul>
</body>
</html>
Essentially, you just want to pull the contents of the
<name/>
element out and put it into an unordered
list. Then you want to pull out the title attribute of
<employee/>
and place it next to the name inside of
a
<em/>
element. To make this happen, you can create an XSLT style sheet:
<?xml version=”1.0”?>
<xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:output method=”html” />
<xsl:template match=”/”>
<html>
<head>
<title>Employees</title>
</head>
<body>
<ul>
<xsl:apply-templates select=”*” />
</ul>
</body>
</html>
</xsl:template>
<xsl:template match=”employee”>
<li><xsl:value-of select=”name” />, <em><xsl:value-of select=”@title”
/></em></li>
472
Chapter 15
18_579088 ch15.qxd 3/28/05 11:42 AM Page 472


JavaScript EditorFree JavaScript Editor     Ajax Editor


©