Level: Advanced
The combined power of XML and XSL for representing, manipulating, and presenting data over the Web
and sharing data across differing applications has been clearly demonstrated through the fast acceptance
and broad usage of these technologies. Still, most developers familiar with the basics of XML and XSL
are not utilizing this power fully. This article shows developers how to use extensions, a technique that
allows you to expand the capabilities of XSL.
In terms both of power and simplicity, the combination of XML and XSL has revolutionized data storage and
manipulation in a way not seen since the early days of the SQL database language. XML provides a clear and
independent way of recoding data that is easily shared and understood. Similarly, many people feel that XSL is also
easy to read, write, and understand. Clearly, this powerful duo are essential knowledge for everyone involved in the
technology industry.
The broad scope and small learning curve associated with the basic elements of XSL transformation sometimes acts as
a double-edged sword -- yielding broad usage of the core technology but dissuading the majority of developers
learning XSL from investigating and using its more advanced and powerful features.
This article is written for developers who already have a basic understanding of XML and XSL, and are ready to build
on this knowledge. If you are unfamiliar with these technologies, you can find several good introductory articles and
tutorials on developerWorks and other Web sites. The article shows you how to use extensions -- a technique present
in most XSL processors -- which allows virtually unlimited expansion of the existing capabilities of XSL's core
features. This article includes a general description of how to write extensions with code, followed by three specific
and widely applicable examples.
Simply put, extensions are a way of calling a method written in some other programming language from within an
XSL document. Usually, the extension methods are written in the same language as that of the XSL processor. There
are exceptions to this rule: Java, for example, can be made to run programs in other languages such as Javascript or
Perl. Thus it is possible to write extensions in XSL in Javascript, Perl, or some other language and make use of them
through a Java-based XSL processor.
What makes these extensions so significant when XSL can already do so much? What XSL gains in simplicity and
broad ability for transformation is often lost in efficiency and ability to do anything unrelated to transformation. For
instance, suppose you have an XML document that lists 5,000 users of your system. The user name, real name, and
e-mail address of each of these users is given under a Users node within the XML. You later append to the XML
document an Interests node in a separate subtree of the XML with user names grouped by particular interests such
as acrobatics, bicycling, computers. You hope eventually to transform the data into an HTML page that groups users
by interests and presents e-mail contacts for people of similar interests. XSL can do this handily with the following
code:
1 of 4 7/15/2008 4:09 PM
Expand XSL with extensions http://www.ibm.com/developerworks/library/x-callbk/index.html?open...
<xsl:for-each select="Interests/Interest">
<b><xsl:value-of select="@InterestName"/></b>
<ul>
<xsl:for-each select="User">
<xsl:variable name="userName" select="@userName"/>
<xsl:variable name="userNode" select="/Root/Users/User[@userName =
$userName]"/>
<li>
<xsl:value-of select="$userNode/@realName"/>
<xsl:value-of select="concat(' ',$userName/@email"/>
</li>
</xsl:for-each>
</ul>
</xsl:for-each>
Unfortunately, the way the transform executes, the entire list of 5,000 users will be examined for each user in each
interest category. This is far more work than you want your server to do for each request to this Web page.
Extensions provide a convenient way around this and several other possible hang-ups that you may encounter when
using XSL on nontrivial data sets. In the above example, a simple hashmap or binary search tree could have easily
solved the problem, but implementing one of these data structures in XSL would be inconvenient and unnecessary.
Extensions to a language that has more appropriate data types will more easily fix the problem. (Incidentally, the code
for this fix is given in the first example below).
Creating an extension
In order to call a method from XSL, that method must first be written and its compiled form placed in the classpath of
the application that is performing the XSL transformation. Methods may be of your own design, supplied by the
standard libraries of Java, or taken from other Java libraries. In some XSL processors, like Xalan, there are even
extension methods written directly into the processor.
The first thing to be aware of when you write or use these methods is the mapping of data types from XSL to Java and
back again. The following table provides a reference to these mappings in Xalan.
2 of 4 7/15/2008 4:09 PM
Expand XSL with extensions http://www.ibm.com/developerworks/library/x-callbk/index.html?open...
3 of 4 7/15/2008 4:09 PM
Expand XSL with extensions http://www.ibm.com/developerworks/library/x-callbk/index.html?open...
4 of 4 7/15/2008 4:09 PM