XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

Automating Stylesheet Creation

Automating Stylesheet Creation

September 07, 2005

Since the early days of XSLT, many have asked whether it was possible to automate the creation of XSLT stylesheets. The general idea of filling out a form or dragging some icons around, then clicking a button and seeing a productive stylesheet generated from your input has always appealed to people. However, the problem of generating working XSLT syntax from the result of someone clicking on pull-down menus and radio buttons has not attracted many takers.

Breaking the problem down into two parts, though, makes it much easier. Step one, the generation of a file like the following from XForms, Tcl/TK, scripting plus an HTML forms interface, or Visual Basic, sounds much more tractable:

<conversion>
  <delete>
    <name>foobarNum</name>
    <name>weather</name>
  </delete>

  <strip>
    <name>email</name>
  </strip>

  <reorder>
    <parent>purchaseOrder</parent>
    <children>
      <name>contact</name>
      <name>shippingAddress</name>
      <name>items</name>
    </children>
  </reorder>

  <wrap>
    <name>shippingAddress</name>
    <wrapper>addresses</wrapper>
  </wrap>

  <attribute2element>
    <name>purchaseOrder/@date</name>
    <elName>orderDate</elName>
  </attribute2element>

  <custom>
    <name>item/@img</name>
  </custom>

</conversion>

If anyone has a successful crack at developing a graphical user interface that creates such a file (and, ideally, reads one, lets the user edit it, and writes out the saved one), let me know and I'll mention it in a future column. Meanwhile, I'll show you how to do step two of the XSLT-generation app: how one stylesheet can create another using the file above (which I've named conversionSpecs.xml) as the source for the generating stylesheet. The tasks that conversionSpecs.xml describes above are somewhat general-purpose for XML transformations, and certainly won't cover all your needs; the stylesheet-generation stylesheet below provides a model for other tasks that you might want to list in a file like the one above and then convert to working XSLT code.

Writing Stylesheet-Generating Stylesheets

The key trick when writing stylesheets that generate other stylesheets is the use of the xsl:namespace-alias element, which I described in further detail in the "Using XSLT to Output XSLT" section of an earlier column titled Namespaces and XSLT Stylesheets. The following shows the beginning of my stylesheet-generation stylesheet. (The complete stylesheet and sample files are available here.) The template rules for the generated stylesheet will have the namespace prefix "wh" in the generating stylesheet, where they're mapped to the dummy namespace "whatever." The xsl:namespace-alias element below tells the XSLT processor to map the "wh" elements to the same namespace as the "xsl" prefix in the generated stylesheet. Because this is the http://www.w3.org/1999/XSL/Transform namespace, this will make the "wh" elements in the generated stylesheet proper XSLT instructions.

<xsl:stylesheet
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:wh="whatever"
            xmlns:exsl="http://exslt.org/common"
            extension-element-prefixes="exsl"
            version="1.0">

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:namespace-alias result-prefix="xsl"
                       stylesheet-prefix="wh"/>

Other parts of the stylesheet setup above include the declaration of a namespace used for a stylesheet extension element, which I'll cover below, and xsl:strip-space and xsl:output elements to make the generated stylesheet a little easier to read, because generated code is often tough on the eyes.

Pages: 1, 2, 3

Next Pagearrow