writing your own website XSLT
the core function of rapple is to process HTML sources files and
generate styled XHTML output. this guide assumes that you have
installed and configured rapple for your needs and that you have a
basic understanding of its usage. it is also assumes that you have
a working familiarity with XML and in particular with XSLT.
the first step to using rapple for a production website is to
design an XSLT template for the site. several example templates are
available in the resources module of the project CVS to inspire you
but they all follow the same basic principles. the important points
are that:
- the XSLT is XHTML aware (note the xhtml namespace);
- that the output settings are appropriate (see the output
method);
- and that the stylesheet expects a "host" parameter.
the "host" parameter refers to domain hosting the website (e.g.,
http://localhost/) and is stored in the rapple configuration file.
this parameter can be referenced as $host within templates thereby
insulating the XSLT from changes (e.g., after building a local test
website you might wish to generate a production site on a different
host - once testing is complete it suffices to change the entry in
the configuration file).
the following example outlines the basic structure of the XSLT
file:
<?xml version="1.0"
encoding="iso-8859-1"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:output method="xml" version="1.0" indent="yes"
encoding="iso-8859-1"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:param name="host"/>
<!-- root template -->
<xsl:template match="/" >
....
</xsl:template>
</xsl:stylesheet>
the real work is done within the root template which is
responsible for generating the output. one popular "design pattern"
is to fill-in-the-blanks. in the example below a simple XHTML page
is generated by extracting the title from the source document
(resolving the CSS file against the host) and performing a shallow
copy of the source contents into the output.
<xsl:template match="/" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head>
<title><xsl:value-of
select="xhtml:html/xhtml:head/xhtml:title"/></title>
<link rel="stylesheet" type="text/css"
href="{$host}style.css"/>
</head>
<body bgcolor="#FFFFFF">
<xsl:apply-templates select="xhtml:html/xhtml:body/*"
/>
</body>
</xsl:template>
<xsl:template match="node() | @*">
<xsl:copy><xsl:apply-templates select="node() | @*"
/></xsl:copy>
</xsl:template>
using the above approach additional elements can be added to the
root template to transform the output. for example footers and
headers can be applied either side of the apply-templates call on
"xhtml:html/xhtml:body/*" or a menu system can be inserted in a
sidebar. in fact it is possible to introduce many interesting
features to this design using pure XSLT solutions. for example see
the context sensitive highlighting for a discussion on how to
generate context sensitive menus.
as you progress you may wish to incorporate external features
into your XSLT using the xsl:include directive. includes can be
used to support:
- definition of global variables to be used by XSLT (e.g., see
librapple.conf.xsl);
- definition of attribute sets used to define default settings on
output elements;
- exposure of "library" templates used across several stylesheets
(e.g., see librapple.xsl).
the xsl:include directive is a top level directive and must
therefore be included just after xsl:stylesheet.
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet ... >
<xsl:include href="library.xsl" >
...
</xsl:stylesheet>
note: in the case of the sablotron xslt processor is it
necessary to provide the absolute path of the included stylesheet
and to preceed it with the "file://" protocol (see faq for
details).
|