Part 1 : XSLT Templates
Templates
In the last chapter, you saw how to take a
well-formed HTML document and turn it into a stylesheet by adding the XSLT
elements <xsl:value-of>
and <xsl:for-each> to pick out
information from a source XML document and produce an HTML result. The
stylesheets that we looked at were simplified
stylesheets. Simplified stylesheets are good as
a starting point when you're creating a stylesheet, and they can be all you
need in some cases. However, to utilize the more sophisticated functionality of
XSLT, you need to use full stylesheets.
In this chapter, we'll take the simplified
stylesheet that we developed during the last chapter and turn it into a full
stylesheet. I'll also introduce you to templates as a way of breaking up your code and look in a bit more detail at
how XSLT processors construct a result from some source XML. You'll learn:
What full stylesheets look like
How the XSLT processor navigates the
source document to create a result
How to break up your code into
separate templates
How templates help with
document-oriented and unpredictable XML
How to create tables of contents in your pages
using template modes
The simplified stylesheets that we used in
the last chapter are a specialized form of stylesheet that make a good starting
point when we're creating an XSLT stylesheet. Simplified stylesheets aren't all
that common in larger applications because they're fairly restricted in what they can do, especially with
document-oriented XML.
Technically,
simplified stylesheets are defined in the XSLT Recommendation in terms of how
they map on to full stylesheets. In the last chapter, we developed the following
simplified stylesheet (HelloWorld.xsl) to
take the Hello World XML document (HelloWorld.xml) and convert it
to HTML:
<?xml
version="1.0" encoding="ISO-8859-1"?>
<html
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xsl:version="1.0">
<head><title>Hello World
Example</title></head>
<body>
<p>
<xsl:value-of select="/greeting" />
</p>
</body>
</html>
The equivalent full stylesheet for
the simplified stylesheet looks very similar. The content of the simplified stylesheet is wrapped in two elements – <xsl:template> and <xsl:stylesheet> – to create HelloWorld2.xsl. The <xsl:stylesheet> element takes
the version attribute and the XSLT
namespace declaration instead of the <html> element, giving the following:
<?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><title>Hello World
Example</title></head>
<body>
<p>
<xsl:value-of select="/greeting" />
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
In the next couple of sections, we'll look
at what these new XSLT elements do.
The document element of a full stylesheet
is <xsl:stylesheet> (a <stylesheet> element in the namespace http://www.w3.org/1999/XSL/Transform – as usual I'm using the prefix xsl here but you could use whatever you liked as long as it's
associated with the XSLT namespace with a namespace declaration). Like the
document element in simplified stylesheets, the <xsl:stylesheet> element needs to declare the XSLT namespace and give the version of XSLT that's used
in the stylesheet with a version
attribute. This time, though, the version
attribute doesn't need to be qualified with the xsl prefix because it already lives on an element in the XSLT
namespace, so the processor knows it's part of XSLT.
You
can also use <xsl:transform> as the document element in a full stylesheet, rather than <xsl:stylesheet>. There is no difference in functionality between the two document elements
– they each use exactly the same attributes and do exactly the same thing. Some
people prefer
to use <xsl:transform> when doing transformations that aren't producing
presentation-oriented formats such as XSL-FO or XHTML. Personally, I use <xsl:stylesheet> all the time.
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.
|
|