The XML SiteStarting to use XML and XSL/XSLT to build web sites.

Starting out with using XML and XSL to build web sites

At the minimum, what we need to create a website is the XML content file and XSLT processing sheet. We will also add an XML schema to that, because it adds structure to the XML file, ensuring correctness. Of course, the files must be placed into a corresponding directory structure and hooked into the automated build scripts.

This is the part I enjoy the most, really. I get easily bored (and annoyed) when writing, but coding is fun! So let's get started.

FoldersCreate the directory tree

  • The framework folder is the root folder and it hosts everything. It will be versioned, so that I'll have a corresponding framework release for each article.
  • The example.com sub-folder will contain the example website. It will be a playground and test bed for the framework.
  • Finally, the www sub folder of the example.com folder will contain the generated web site's files (HTML, images, CSS, etc.).

Create the files

I need three files for the example website, all in the example.com folder:

  • XML Schema
  • XML content file
  • XSLT processing sheet

Create the XML Schema

This file will contain the XML schema for the example website. I'll call it example.xsd. The great advantage to using a schema is that it will enforce the validity of the XML file, greatly reducing the chance of errors in the content.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

</xs:schema>

Create the XML content file

This file, called example.xml, will be empty for now. I will complete it after I create a minimal schema in example.xsd.

<?xml version="1.0" encoding="UTF-8"?>

Create the XSLT processing sheet

I will use XSLT version 2.0, it's not finished yet but several of the features I need are already defined and also implemented by Saxon, our choice of XSLT processor.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  
</xsl:stylesheet>

Set up the automated processing

This is the build.xml ant build file, also residing in the example.com folder.

<?xml version="1.0" encoding="UTF-8"?>
<project name="Example.com Website" default="build" basedir=".">
  <target name="build" description="Build website.">
    <xslt style="example.xsl" in="example.xml" out="www/out.html"/>
  </target>
  <target name="clean" description="Clean build."/>
</project>

I implemented two targets, one for building the website and another one for cleaning the build. Running it right now will give an error because the XML file was left empty, but everything else should be in order.

Download: article files.

Read on: XML Schema and XSL templates for a web site.

First Posted: October 28th, 2005 - Friday.
Last Updated: November 13th, 2005 - Sunday.