Tag libraries and callbacks for XSLT / XQuery

At the Balisage conference earlier this month, Vyacheslav Zholudev gave quite an interesting talk on virtual documents. That spurred a related thought: could XSLT / XQuery support an equivalent to JSP tag libraries?

Background: If you aren’t familiar with tag libraries, the goal is a clean division of responsibilities:

Page designer
Implements the layout and styling by embedding tags in HTML pages and applying CSS to the data produced by the tags.
Tag developer
Implements the data access and structuring by defining tags that query data stores, sort query results, and so on.

A typical page using tags has fragments like:

<h1>Attendees in 2010</h1>
<table class="attendees">
  <tr><th>Last Name</th>
    <th>First Name</th></tr>
<db:person-list select-year="2010" sort-by="last-name">
  <tr><td class="lname">{ $last-name  },</td>
    <td class="fname">{ $first-name }</td></tr>

The result after processing the db:person-list tag is something like:

<h1>Attendees in 2010</h1>
<table class="attendees">
  <tr><th>Last Name</th>
    <th>First Name</th></tr>
  <tr><td class="lname">Crawford,</td>
    <td class="fname">Randy</td></tr>
  <tr><td class="lname">Franklin,</td>
    <td class="fname">Aretha</td></tr>
  <tr><td class="lname">Green,</td>
    <td class="fname">Albert</td></tr>

XSL Pages: XSLT Simplified Stylesheet Modules already provides most of constructs of the JSTL core library. XSLT value templates offer a parallel to the Java EL expression language. The additions necessary for a complete story:

  • Associating a tag name with a function or template implementation in an XSLT / XQuery module.
  • Importing the tag library module by allowing xsl:import inside the root element (that is, html) of a simplified stylesheet page.
  • Supporting XPath expressions in content by allowing value templates outside attributes in simplified stylesheet pages.
  • Processing tags in simplified stylesheet pages as calls to the associated function or template, passing attributes as parameters.
  • Procesing the tag content during execution of the function or template as a callback (for instance, executing the tr fragment for each result produced by the db:person-list tag.)

Callbacks pose the only major addition, but they would have general benefit for XSLT / XQuery. For instance, when converting between XML vocabularies, the optimal decision often requires knowledge of the current source context and result context. Callbacks could offer a natural solution to that problem.

To evaluate the idea, I wrote transforms for XSLT pages and tag libraries conforming to the sketch above (using Dimitre Novatchev’s anonymous function technique from FXSL for the callbacks). Running the transforms on a test case, I can produce an HTML page using XSLT tag libraries.  Here’s the experimental package.

Conclusions: The experiment teased out some refinements (such as being able to provide parameters as subelements) and provides a proof of concept, I think, of the benefits of tag pages and tag libraries for XSLT / XQuery. However, I may not do much more with these conversion transforms:

  • I don’t see a way for the callbacks to read the variables that were in scope at the tag call (along the lines of Java anonymous local classes). Maybe Novatchev is right and XSLT / XQuery needs an anonymous functions constructor (after all, JavaScript has one) or maybe a more restrictive callback feature would do the trick.
  • Tag libraries must be implementable in XQuery as well as XSLT. For XQuery, tag libraries could provide a set of building blocks for rapid application development against an XML store.  For XSLT, a possible scenario would be composing transforms by mocking up the output document.

Anyway, many thanks to Vyacheslav for stimulating the thought.

Two existing initiatives have some relevance:

  • Cocoon eXtensible Server Pages (XSP) support pipelines that format data from multiple sources using XSLT transforms.
  • Resin XML Template Pages (XTP) applies XSLT stylesheets to what appear to static placeholder tags within XHTML files. To get the full power of tag libraries, the tags should instead be executed (or interpretted).
This entry was posted in XQuery, XSLT. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s