Latest ESXX Release


Thursday, February 5, 2009

Server-side XML processing, the ESXX way

I've continued to add more documentation to the wiki. Let's talk about server-side XML processing via XSL this time.

With all due respect to command line applications, ESXX was designed for web applications.

An ESXX web application is defined by an XML file and zero or more JavaScript handlers or XSLT stylesheets. The XML file may be any XML file, and it will be available as an E4X node in the esxx.document variable. There are a few special constructs that may be present in it:
  • Other XML documents may be included using XInclude.
  • An <?esxx-stylesheet?> processing instruction may be specified to instruct ESXX to transform the document using the specified XSLT 2.0 stylesheet.
  • One or more <?esxx-include?> processing instructions may be used to load JavaScript code into memory.
  • One or more <?esxx?> processing instructions can also be used to specify JavaScript code in-line.
  • Various handlers may be installed by including elements from the http://esxx.org/1.0/ namespace in the document.

The most common situation is simply an http://esxx.org/1.0/ describing the handlers, plus <?esxx-stylesheet?> and <?esxx?> to set up the JavaScript environment, but let's look at a few examples first.

Plain XML
ESXX can serve static XML files. The file will be parsed, stored in esxx.document and serialized on every request. Hardly useful, but possible.

<?xml version="1.0"?>
<xml>Hello from a totally useless ESXX application!</xml>

XSLT transformation of static XML documents
Perhaps a bit more realistic, ESXX can be instructed to transform an XML document on each request. For instance, assume you have a DocBook document that you'd like to serve as HTML on the web. Instead of converting the document by hand every time you modify it, let ESXX serve and transform it using the XSLT 2.0 stylesheet specified by an <?esxx-stylesheet?> processing instruction in the document.

ESXX will load and parse the DocBook document and the stylesheet, and the compiled stylesheet will be executed on every request. If either of the DocBook document or the XSLT stylesheet is modified, the application will automatically be reloaded, which actually makes this a nice way to preview DocBook documents you're working on.

Here's a simple (non-DocBook) example:

<?xml version="1.0"?>
<?esxx-stylesheet type="text/xsl" href="useful.xslt"?>
<doc>
<para>Hello from a somewhat useful ESXX application!</para>
</doc>
And the stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output
doctype-public="-//W3C//DTD XHTML 1.1//EN"
doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
method="xhtml" version="1.0" media-type="text/html" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="/doc">
<html>
<body>
<xsl:apply-templates/>
</body>

</html>
</xsl:template>

<xsl:template match="para">
<p>
<xsl:apply-templates/>

</p>
</xsl:template>
</xsl:stylesheet>
JavaScript and XSLT handlers

Stay tuned ...

No comments: