Converting HTML to XSLT for easier templating

After all the recent discussions on easier templating, here's an experiment based on Conal Tuohy's html-to-xslt transform (mucho thanks Conal), enhanced with template rules which make it easy to process elements without knowing where they appear in the input (like xsl:templates, which is what they are converted to).

The use case is the conversion of one of our xdoc files, I tried with our userdocs/forms/binding.xml document as an example input (find it here) and the results are pretty decent already, although the HTML template is very easy to understand.

This would be applicable to any output format, not only HTML.

I'm putting the source files inline instead of attaching them, in case people want to improve them we can take advantage of the wiki versioning to keep track of changes.

See Also

The HTML template

Here's my example template, it converts to above binding.xml xdoc document to HTML when used with the XSLT transform and sitemap shown below.

By converting to XSLT instead of implementing our own thing, we keep all the power of XSLT, but this is much easier to write and explain than an actual XSLT transform.

{{{<!--

-->

<html>

</html> }}} Simple, not? That's all one must write to convert the XML document to HTML.

The XSLT transform

This is based on Conal Tuhoy's transform, it transforms the above HTML template into an XSLT transform.

I have enhanced it to generate addition xsl:template constructs for individual elements based on the <div id="atl-templates"/> section of the HTML template.

The this-xsl namespace is necessary to differentiate between actual XSLT transform instructions and XSLT instructions that must be generated in the output.

{{{<this-xsl:stylesheet version="1.0"

</this-xsl:stylesheet> }}}

The sitemap

In case you want to try this at home, here's the (dead simple) sitemap. {{{<?xml version="1.0"?>

<!-- atl-proto samples sitemap -->

<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">

</map:sitemap> }}}

HtmlToXsltExperiments (last edited 2009-09-20 23:42:36 by localhost)