Introducing XWeb

Using your own XML

Using XWeb for XHTML files allows you to separate the aspects of navigation, layout and textual content in your website, but there is another step you can do to handle your site in an easier way: use XML formats specific for your data.

Let's consider a simple example: you want to put your CV on your private site. Of course this can be written in XHTML like this:

  <h1>Curriculum Vitae</h1>
  <h2>Education</h2>
  <dl>
    <dt>01/01/01 - 03/03/03</dt>
    <dd>Visited some school</dd>
  </dl>

But a format like this is not easy to maintain since it does not capture the structure of the data directly. Whenever you add a new item you have to make sure that you keep consistency in the layout and if you decide to try another layout, maybe using tables instead of the description lists, you will have to change the whole file.

The solution for this problem is to use a data specific XML format. You can either try to find a format someone already defined for the kind of data you have (e.g. BibTeXML for publications or others you can find in special databases) or you just make up your own format, with or without DTD or XML Schema definition.

Here is a simple way to write down the information in a CV as XML formatted file:

  <cv>
    <section title="Education">
      <item>
        <time>01/01/01 - 03/03/03</time>
        <description>Visited some school</description>
      </item>
    </section>
  </cv>

There are no hard rules how to define these formats, how to call elements and attributs or which parts to model as attributes, which as elements. Here is another version modelling the same data:

  <curriculumVitae>
    <group>
      <title>Education</title>
      <event time="01/01/01 - 03/03/03">Visited some school</event>
    </group>
  </curriculumVitae>

And there are many more like splitting the time down into start/end, maybe day/month/year or adding a description to the sections/groups. Here are some rules of thumb that might be useful:

  • Try to give meaningful names, don't call everything <item>, <element> or similar. Try to give the XML you wrote to someone else and ask if this is easy to read without explanations.
  • If you are unsure if something should be attribute or child element, try evaluating the two sentences: "X is an attribute of Y" and "X is a part of Y" -- this is often (but not always) helpful.
  • Don't care too much about the details if it is just for your website -- it is very easy to turn attributes into elements, turn elements into attributes or change names using XSLT.

Once you have decided which format to use, you need some XSLT code to change this into XHTML. Once you have the stylesheet for turning your format into XHTML you can give the result of this process into the normal layout stylesheet to be turned into a page of your site. This two-step process allows reusing the layouting part on one hand and the transformation into XHTML form your XML on the other: you can always create a normal XHTML page from your XML file using the same stylesheet you use for XWeb.

The way XWeb handles this is called an XSLT-chain. You can tell XWeb to first call one stylesheet on your files, afterwards the result of this process will be used as input for another stylesheet. Usually this is used for first turning specific XML into XHTML and then layouting this, but you can build arbitrary XSLT-chains with different stylesheets and with more than two involved. The only important thing to care about is that each stylesheet that produces input for a next one has to output XML -- otherwise the next stylesheet can't accept the input. The last stylesheet in the chain can produce whatever you want it to produce from XML via HTML to plain text or even binary information.

XWeb will use an XSLT chain whenever you put another <xsl> element into an existing <xsl> element like this:

  <xsl stylesheet="stylesheets/layout.xsl" navigationElement="html">
    <xsl stylesheet="stylesheets/cv2xhtml.xsl"/>
  </xsl>

You can nest other stylesheets into the inner one, each of them can have <parameter>s attached and each of them is allowed to use the navigationElement attribute. XWeb will first call the inner stylesheet with the parameters attached to the inner <xsl> element, then use the result as input for the outer stylesheet, using the parameters on this level.

Here is a simple stylesheet that can be used for turning the first XML version of the example into the HTML version:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  
  <!-- match root element -->
  <xsl:template match="cv">
    <!-- create HTML main parts -->
    <html>
      <head>
        <title>Curriculum Vitae</title>
      </head>
      <body>
        <h1>Curriculum Vitae</h1>
        <!-- process all sections -->
        <xsl:apply-templates select="section"/>
      </body>
    </html>
  </xsl:template>
  
  <!-- called for each section -->
  <xsl:template match="section">
    <!-- give title attribute as header -->
    <h2><xsl:value-of select="@title"/></h2>
    <!-- create list with all items in this section -->
    <dl>
      <xsl:apply-templates select="item"/>
    </dl>
  </xsl:template>
  
  <!-- called for all items -->
  <xsl:template match="item">
    <!-- put time and description as title and text into the decription list -->
    <dt><xsl:value-of select="time"/></dt>
    <dd><xsl:value-of select="description"/></dd>
  </xsl:template>
  
</xsl:stylesheet>

The "homesite" example in the distribution uses this approach for the CV, adding parameters to determine the language and splitting the dates into the subparts to give localized date formats.