Tag Libraries
for XSLT and XQuery

Erik Hennum, MarkLogic Inc.
Vyacheslav Zholudev, Jacobs University Bremen
August 2, 2011

Tag libraries in a nutshell

A Java technology for dynamic web pages:

Lots of adoption, lots of tag libraries

The question on the table

or


Tag libraries don't change what you can do with XML
but who can tap into XML technology

Canonical example: the tagdoc (1 of 2)

<html:table>
    <tag:attendees>
        <html:tr>
            <html:td>${last-name}</html:td>
            <html:td>${first-name}</html:td>
        </html:tr>
    </tag:attendees>
</html:table>

Canonical example: the result document (2 of 2)

<html:table>
        ...
        <html:tr>
            <html:td>Fitzgerald</html:td>
            <html:td>Elena</html:td>
        </html:tr>
        <html:tr>
            <html:td>Green</html:td>
            <html:td>Alfonso</html:td>
        </html:tr>
        <html:tr>
            <html:td>Hutz</html:td>
            <html:td>Egberto</html:td>
        </html:tr>
        ...
</html:table>

Why would you care?

Expertise level vs number of practitioners

A closer look at a tag call

<c:set var="feed-uri"
       value="http://news.ycombinator.com/rss"/>
<feed:summary feed="${feed-uri}" ...>
  <p>
    <a href="${feed-uri}">${title}</a> ${description}
  </p>
</feed:summary>

pull - <feed:summary> tag is a call from the tagdoc to the taglib

push - the tag content is a callback from taglib to tagdoc
with ${title} and ${description} parameters

closure - the tag content refers to ${feed-uri} variable
defined before the tag call

taglib as DSL (Domain Specific Language)

General-purpose language with programming syntax
- do anything

ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
    String lastName = rs.getString("LAST_NAME");
}

Domain-specific language with custom statements
- do one thing clearly (semantic transparency)

SELECT LAST_NAME, FIRST_NAME FROM ATTENDEES

Comparison with Simplified Stylesheets

Compatibility with XForms

XSTag (XML Simple Tags)

Workflow

Adapting Java Tag Libraries for the XML toolkit

Java XSTag
Expressions Unified Expression Language XPath
Tag bindings Configuration files QName of function or template
Callbacks One unnamed
tag body
Many named handlers
Fragment reuse Document inclusion Basic tag libraries

Fragment reuse with a basic tag library

<-- A basic taglib that defines page components -->
<xst:taglib version="0.2" ... xmlns:pg=".../pagecomp">
  ...
  <xst:tag name="pg:footer" as="element()*">
    <p>Copyright 2011, Some Legal Entity</p>
  </xst:tag>
</xst:taglib>
<-- A tagdoc that uses the taglib to generate a page -->
<html xst:version="0.2" ... xmlns:pg=".../pagecomp">
  <xst:import href="PageComponents.xstag"/>
  ...
  <body>
    ...
    <pg:footer/>
  </body>
</html>

Example of feed processing -
demo and source

A basic taglib for feed iteration

<xst:taglib version="0.2" ... xmlns:feed=".../feed/">
... other tags get feed and properties ...
<xst:tag name="feed:list" as="node()*">
  <xst:param name="feed" as="document-node()*"/>
  <xst:param name="onItem" implicit="true" as="handler(
        $title as xs:string?,
        ...
        ) as node()*"/>
  <xst:for-each name="item" at="number" select="...">
    ...
    <xst:call handler="$onItem">
      <xst:with-param name="title" select="..."/>
      ...
    </xst:call>
  </xst:for-each>
</xst:tag>
</xst:taglib>

A plain HTML tagdoc (1 of 4)

<html xst:version="0.2" ... xmlns:feed=".../feed/">
<xst:import href="feed-lib/feeds.xstag"/>
<xst:variable name="feed">
  <feed:get>http://news.ycombinator.com/rss</feed:get>
</xst:variable>
...
<body>
  <h1><feed:title feed="{$feed}"/></h1>
  <ul>
    <feed:list feed="{$feed}" xst:handles="$uri, $title">
      <li><a href="{$uri}">{$title}</a></li>
    </feed:list>
  </ul>
</body>
</html>

A tagdoc with feeds in a widget taglib (2 of 4)

<html xst:version="0.2" ... xmlns:widget=".../widget/">
<xst:import href="feed-lib/feeds.xstag"/>
<xst:import href="widget-lib/widgets.xstag"/>
...
<body>
  ...
  <widget:accordion-list>
    <feed:list feed="{$feed}" xst:handles="$uri, $title, ...">
        <widget:accordion-summary>
          {$title}
        </widget:accordion-summary>
        <widget:accordion-detail>
          <p>Source: <a href="{$uri}">{$uri}</a> ... </p>
        </widget:accordion-detail>
    </feed:list>
  </widget:accordion-list>

An AJAX client tagdoc (3a of 4)

<html xst:version="0.2" ... xmlns:rfsh=".../refresh/">
<xst:import href="refresh-lib/refresh.xstag"/>
...
  <rfsh:refresh>
    <rfsh:trigger>feed-view</rfsh:trigger>
    <rfsh:provider>feed-ajax-server.html</rfsh:provider>
    <rfsh:params>feed-uri=feed-select</rfsh:params>
    <rfsh:targets>feed-title, feed-list</rfsh:targets>
  </rfsh:refresh>
...
  <form action="#">...
    <select id="feed-select">...</select>
    <button id="feed-view" type="button">View</button>
  </form>
  <h1 id="feed-title"/>
  <ul id="feed-list"/>
...

An AJAX server tagdoc (3b of 4)

<div xst:version="0.2" ... xmlns:feed=".../feed/">
<xst:import href="feed-lib/feeds.xstag"/>
<xst:param name="feed-uri">...</xst:param>
<xst:variable name="feed">
  <feed:get>{$feed-uri}</feed:get>
</xst:variable>
  <h1 id="feed-title"><feed:title feed="{$feed}"/></h1>
  <ul id="feed-list">
    <feed:list feed="{$feed}" xst:handles="$uri, $title">
      <li><a href="{$uri}">{$title}</a></li>
    </feed:list>
  </ul>
</div>

An XSL-FO tagdoc (4 of 4)

<root xst:version="0.2" ... xmlns:feed=".../feed/">
<xst:import href="feed-lib/feeds.xstag"/>
...
    <block ...>Table of Contents</block>
    <feed:list feed="{$feed}" xst:handles="$number, ...">
      <block ...>
        <basic-link ...>{$title}</basic-link>
        ...
        <page-number-citation ref-id="f{$number}"/>
      </block>
    </feed:list>
    ...
    <feed:list feed="{$feed}" xst:handles="$number, ...">
      <block id="f{$number}" ...>{$title}</block>
      ...
    </feed:list>
...

Processor-specific tag libraries

Concluding points

Limitations

Glue for component configuration and assembly -
not for:

Open questions

Summary: a collaboration strategy

Preprocessor implementation for experiments at
http://code.google.com/p/xstag