Re: [xsl] Generic XSLT stylesheet to generate pages....Ideas please...

Subject: Re: [xsl] Generic XSLT stylesheet to generate pages....Ideas please...
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 26 Feb 2002 17:33:52 +0000
Hi Edward,

> I am trying to come up with a generic XSLT stylesheet which takes in
> XTML (Page definition), XML (Data), XSLT(standard call_templates for
> page components) and generates either HTML/XHTML in one go.
> Alternatively the XHTML(Page definition) could be read in without
> the XML(Data) to produce an XSLT stylesheet which when applied to
> the XML (Data) produces XHTML/HTML in a second step. My gut feeling
> in to produce the resultant XHTML in go since one is effectively
> merging XML documents to produce a resultant XML document.

One way of doing this is to have the page definition XHTML contain
elements that indicate where you want the XML data to be inserted. The
placeholder elements can be more or less complex as you require, but
the more complex they are, the more likely that you're reinventing
XSLT and you may as well use a simplified stylesheet rather than a
separate page definition.

For example, a simple page definition (template.xhtml) might look

<html xmlns="";
    <title>Test Page</title>
      Username: <ins:username />

With a XML document (data.xml) being:

  <username>Edward Bedell</username>

A stylesheet could accept one or the other of these as inputs, or even
just use itself as the source, but it should store both the template
and the data as global variables and apply templates to the
template.xsl document. For example:

<xsl:stylesheet version="1.0"

<xsl:variable name="template" select="document('template.xsl')" />
<xsl:variable name="data" select="document('data.xml')" />

<xsl:template match="/">
  <xsl:apply-templates select="$template/*" />



The majority of the result is created by copying the XHTML elements
from the template document. For this, you need an identity template
that matches all the nodes in your document and just copies them:

<xsl:template match="node()|@*">
    <xsl:apply-templates select="@*|node()" />

The exceptions to this processing are the templates in your namespace,
which are placeholders indicating where information from the data
document should be inserted in the template page. For this example,
the ins:username element means that the value of the username element
child of the info element in the data document should be inserted. So
you should have a template matching that instruction and doing what it

<xsl:template match="ins:username">
  <xsl:value-of select="$data/info/username" />

You can keep adding these templates - one per placeholder or
instruction that you place in the page template.

You can do it the other way you suggested as well - use the page
definition to create a stylesheet that you then use to process the XML
document - but it's slightly more complicated because you have to mess
around with namespace aliases and so on. However, it does have the
advantage over the above method that it means your
placeholder/instruction elements can contain XPaths, since you aren't
limited by the fact that you can't evaluate an XPath on the fly using
XSLT. I believe that creating a stylesheet from a page definition in
this way is a form of literate programming...



Jeni Tennison

 XSL-List info and archive:

Current Thread