Anda di halaman 1dari 4

Expand XSL with extensions http://www.ibm.com/developerworks/library/x-callbk/index.html?open...

Expand XSL with extensions


Technique helps expand the capabilities of XSL's core features

Level: Advanced

Jared Jackson (jjared@almaden.ibm.com), Research Associate, IBM


01 Apr 2002

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.

What are XSL extensions?


It must first be understood that XSL, like all other programing languages, is merely a grammar specification in need of
an implementation. Fortunately, XSL has become very popular and there are several implementations to choose from.
Extensions are not a required feature of the grammar and, thus, their syntax is not as well defined as the other
constructs of the language. They are, however, now included in the W3C's XSLT Recommendation. The examples in
this article will follow the format of that recommendation.

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...

Listing 1. User interest XSL transformation without extensions

<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).

Technologies used in this article


It would be a daunting task to list all of the XSL processors and their methods for implementing extensions. This
article uses the Java version of Xalan -- a popular and freely available XSL processor from the Apache Project -- to
describe the specifics of writing extensions. All of the examples are targeted to that platform. (Xerces, another Apache
product, is used as the XML parser. You can download Xalan and Xerces from links in Resources.) Most other popular
XSL implementations also provide a mechanism for extensions, but you'll need to consult their documentation to find
any differences in approach.
To simplify working with XML and XSL, I have also provided Java code for some of the more common XML
manipulations. This code, along with the code and data necessary to run all of the examples, is provided in a zip file in
Resources. This file does not, however, include external libraries such as Xalan and Xerces. After you obtain those
libraries by following links in Resources (versions: Xalan - Java 2.3.1; Xerces 1.4.4), place their jar files in the lib
directory extracted from the zip file. For those readers who wish to jump directly to the examples, all Java code is in
the src directory, XML data in the XML directory, XSL transforms in the XSL directory, batch files in the bin
directory, and compiled code in the lib directory.

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.

Tables 1,2. Data Type Mappings

2 of 4 7/15/2008 4:09 PM
Expand XSL with extensions http://www.ibm.com/developerworks/library/x-callbk/index.html?open...

Parameter Mapping Return Type Mapping


XSLT Java Type XSLT Type
Java Type
Type org.w3c.dom.traversal.NodeIterator
Node Set org.w3c.dom.traversal.NodeIterator org.apache.xml.dtm.DTM
String java.lang.String org.apache.xml.dtm.DTMAxisIterator Node Set
org.apache.xml.dtm.DTMIterator
Boolean java.lang.Boolean org.w3c.dom.Node
Number java.lang.Double java.lang.String String
Result java.lang.Boolean Boolean
Tree org.w3c.dom.DocumentFragment
Fragment java.lang.Number Number
org.w3c.dom.DocumentFragment Result Tree Fragment

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

Anda mungkin juga menyukai