Saxonica: XSLT and XQuery Processing: Design Approach

Design Approach

The design approach behind Saxon-CE is twofold:

  1. You should be able to do most things in XSLT without writing any JavaScript

  2. If you do have to write any JavaScript, or make use of existing JavaScript components, the interface should be as seamless as the specifications allow.

As far as rendering XML is concerned, you can start with the traditional approach of writing a transformation that takes an XML document as input, and produces an HTML page as output. In this case, the xml-stylesheet processing instruction is used to invoke a small 'boot' stylesheet that loads the Saxon-CE processor before loading the main stylesheet.

For newer projects, where it's possible to make changes that exploit Saxon-CE's capabilities more fully, several additional strategies are available: Since there is an HTML page, it makes sense to use it to create the page framework - the structure of sections and images that will then be populated by content. The stylesheet can then use the XSLT 2.0 xsl:result-document instruction to create as many pieces of content as it wishes, posting each one to a separate container in the HTML page framework.

At this stage you would traditionally include attributes such as onclick in the generated HTML, each onclick providing a JavaScript event handler to process user input. With Saxon-CE, however, there's a higher-level approach: you can define template rules in the stylesheet that process these events. Each template rule defines the set of HTML elements that it applies to (in its match attribute), and the set of events that it applies to (in its mode attribute: a set of standard mode-names such as ixsl:onclick is provided for this purpose.)

These event-handling template rules can create or modify chunks of content in the HTML page. The new content can be defined in the same way as the original content, using the XSLT 2.0 xsl:result-document instruction. The stylesheet can also modify individual attributes of HTML elements (for example, changing the visibility to "hidden") using the extension instruction ixsl:set-attribute. This is done without damaging the integrity of XSLT as a declarative language, because the updates are all deferred until each event has been fully processed: the stylesheet sees an immutable input structure, and generates an output which is a set of changes to be applied later.

It is also possible to fire off templates for execution at a later time, thus allowing animations to be constructed.

Where more detailed interaction with the browser platform is required, Saxon-CE provides a set of extension functions allowing full manipulation of JavaScript objects from within any XPath expression. One of the demonstration applications, for example, shows how this can be used to read the current mouse coordinates as input to a decision on how to render the HTML page.

Next