Inside the <xsl:stylesheet> document element, XSLT stylesheets are made up of a number of
templates, each of which matches a particular part of the source XML document
and processes it whatever way you define. The templates are rules that define how a particular part of the source XML document
maps on to the result that you
want. Thus a full stylesheet has a structure that's quite similar to the
structure of CSS stylesheets – a set of rules that match different elements and
describe how they should be presented.
There
are some very fundamental differences between CSS stylesheets and XSLT
stylesheets, though. First, while CSS always processes all the elements in a
document, you can use XSLT to pick and choose which elements to display.
Second, while multiple rules can be applied to style a particular element in
CSS, only one template can be applied at a time in XSLT. Third, XSLT templates
can match a lot of things that CSS templates can't, such as attributes and
comments.
Templates are defined using the <xsl:template> element. The match
attribute on <xsl:template>
indicates which parts of the source document should be processed with the particular template and the content of the <xsl:template> element dictates what is done with that particular part of the source document. You can use literal result elements, <xsl:value-of>, and <xsl:for-each>
inside a template in exactly the same way as you do within a simplified
stylesheet to generate some output.
A full XSLT stylesheet has an <xsl:stylesheet> document element, which contains a number of <xsl:template> elements, each of which defines the processing that should be
carried out on a particular part of the source XML.
In this section, we'll convert the
simplified stylesheet TVGuide.xsl that we created in the last chapter into a full stylesheet and test that the full stylesheet gives exactly the same result for TVGuide.xml as the simplified stylesheet did.
The simplified stylesheet TVGuide.xsl looks as follows:
<?xml
version="1.0" encoding="ISO-8859-1"?>
<html
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xsl:version="1.0">
<head>
<title>TV Guide</title>
<link rel="stylesheet" href="TVGuide.css"
/>
<script type="text/javascript">
function toggle(element) {
if (element.style.display == 'none') {
element.style.display = 'block';
} else {
element.style.display = 'none';
}
}
</script>
</head>
<body>
<h1>TV Guide</h1>
<xsl:for-each select="/TVGuide/Channel">
<h2 class="channel"><xsl:value-of
select="Name" /></h2>
<xsl:for-each select="Program">
<div>
<p>
<span class="date"><xsl:value-of
select="Start" /></span><br />
<span class="title"><xsl:value-of
select="Series" /></span><br />
<xsl:value-of select="Description" />
<span onclick="toggle({Series}Cast);">[Cast]</span>
</p>
<div id="{Series}Cast" style="display:
none;">
<ul class="castlist">
<xsl:for-each
select="CastList/CastMember">
<li>
<span class="character">
<xsl:value-of select="Character" />
</span>
<span class="actor">
<xsl:value-of select="Actor" />
</span>
</li>
</xsl:for-each>
</ul>
</div>
</div>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
When you use this stylesheet with TVGuide.xml, you get the following display:
To create a full stylesheet from this
simplified stylesheet, you need to do the following:
Add an <xsl:template> element whose match attribute has the value / around the <html> element
Add an <xsl:stylesheet> element around the new <xsl:template> element
Move the XSLT namespace declaration
from the <html> element
to the <xsl:stylesheet> element
Remove the xsl:version attribute from the <html> element and add an equivalent version attribute on the <xsl:stylesheet> element
The result of these four steps is TVGuide2.xsl, with the following outline:
<?xml
version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template
match="/">
<html>
<head>
...
</head>
<body>
...
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Now run the transformation with TVGuide.xml, this time with the full stylesheet TVGuide2.xsl. Do this by amending the xml-stylesheet processing instruction in TVGuide.xml as follows:
<?xml-stylesheet
type="text/xsl" href="TVGuide2.xsl"?>
You should see exactly the same result as
you had before.
Buy Beginning XSLT here
© Copyright 2002 Wrox Press
This chapter is written by Jeni Tennison
and taken from "Beginning XSLT" published by Wrox Press Limited in June 2002; ISBN 1861005946; copyright © Wrox Press Limited 2002; all rights reserved.
No part of these chapters may be reproduced, stored in a retrieval system or transmitted in any form or by any means -- electronic, electrostatic, mechanical, photocopying, recording or otherwise -- without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.
|
|