As seen over the past couple of columns, XSLT 1.0 provides good text transformation functionality, but doesn't really go far enough to provide good data manipulation capabilities. To be fair, it was never intended to do so; as XSLT 1.0 was being designed, its creators couldn't have known the convoluted uses it would be put to. However, the XSLT 1.0 specification does provide a mechanism for extending the functionality of XSLT through additional elements and functions. The relevant passages from the specification are these:
The element extension mechanism allows namespaces to be designated as extension namespaces. When a namespace is designated as an extension namespace and an element with a name from that namespace occurs in a template, then the element is treated as an instruction rather than as a literal result element. The namespace determines the semantics of the instruction.
If aFunctionNamein aFunctionCallexpression is not anNCName(i.e. if it contains a colon), then it is treated as a call to an extension function. TheFunctionNameis expanded to a name using the namespace declarations from the evaluation context.
This means that if XSLT processor developers want to add more functionality, they are free to do so -- as long as the elements and function names that support that functionality appear in their own namespace. One of the first major efforts to take advantage of this feature was Mike Kay's Saxon XSLT processor: By including elements and attributes from the saxon: namespace, developers could create style sheets that performed complex manipulation of source documents. The downside was that the style sheets were no longer portable: They did not function correctly in non-Saxon processors. Similarly, some Java implementations of XSLT extended the functionality by allowing Java calls directly from style sheets using the java: namespace -- but on processors that did not support elements and attributes from this namespace, the style sheets did not work correctly.
To make XSLT extensions more portable, a group of developers have created the EXSLT specification. This is an attempt to identify useful extensions to XSLT 1.0 and to partition them into functional groups that can be implemented on an á la carte basis. The EXSLT effort is an open one; anyone who wishes to contribute may do so.
The modules currently defined for EXSLT are:
- Dates and Times provides a broad spectrum of date manipulation functionality, including calculating the difference between two dates and retrieving the local system current date and time.
- Dynamic provides a mechanism for the processing of dynamically-generated XPath expressions to accomplish various tasks, including retrieving values and selecting minimum and maximum values.
- Common provides a few basic extension functions, such as a mechanism for the creation of multiple output files and a function that returns a string containing the object type of an object passed into it.
- Functions provides a mechanism for users to define their own functions.
- Math provides simple math functions, such as trigonometric functions and logarithmic functions.
- Regular Expressions provides regular expression processing functionality, including a function that selects substrings of a string that match a given regular expression and a function that replaces matching substrings with a different literal value.
- Sets provides functions for nodeset manipulation, including returning the intersection of two nodesets and returning nodes that appear in one nodeset but not another.
- Strings provides some very useful string manipulation functions, such as a function that tokenizes a string into a nodeset of token elements and a function that concatenates the string values of all the nodes in a nodeset.
Because EXSLT hasn't been formally accepted by any standards bodies, implementation of the functionality in EXSLT tends to be hit-and-miss. There are several different ways these functions may be implemented, listed here in increasing order of portability:
XSLT templates. Many of the functions in EXSLT are simply shortcuts to equivalent behaviors that can be achieved using XSLT templates. For these functions, the EXSLT authors have provided XSLT templates that may be imported into existing style sheets. This approach offers the most portability, as all XSLT 1.0-compliant processors are able to correctly interpret the template and provide the functionality.
EXSLT functions. If the XSLT processor you are using supports the function element from the function namespace in XSLT, you can take advantage of the EXSLT function implementations that are provided for some functions. This approach is a little less portable than XSLT templates, but you can use the function as part of other expressions in your style sheets.
Language-specific implementations. For some functions that are simple enough to be implemented in languages like JavaScript, implementations are available that you can include in processors that support those languages. Functions such as the ones in the math namespace are ideal for this.
Native implementations by specific processors. The most functionality can be offered by processors that include native support for EXSLT functions. Some EXSLT functions, such as those in the dynamic namespace, can only be implemented natively.
Depending on which functions you need to use and the amount of portability that your style sheets require, any of these solutions might be the best fit for your needs. You can find full documentation on the implementation status of each function or element in EXSLT at the EXSLT Web site.
So is EXSLT a good idea for you?
As usual, whether EXSLT is right for a particular programming task really depends on your particular circumstances. If you are writing style sheets that may be locked into a particular processor platform (such as style sheets used to transform documents on a server), then you may be able to select a processor that implements the extension functions and elements you need. If you need portable style sheets (for client-side styling, for example, or in a multiple XSLT processor environment), then you may need to limit your use of XSLT to those functions that you can implement with XSLT templates. The good news is that XSLT 2.0 contains many functions and elements that are similar, or identical, to those found in EXSLT -- so if you invest in EXSLT implementations now, porting them to XSLT 2.0 should be a relatively simple task. You could even use a style sheet to transform EXSLT style sheets into XSLT 2.0 style sheets -- but that's another column.
- Learn more about EXSLT at the EXSLT Web site.
- Download Michael Kay's Saxon processor.
- Download the 4XSLT processor, part of the 4Suite platform for XML and RDF processing.
- Download the libxslt processor.
- Download the Xalan-Java processor from Apache.
- Discover why Kevin advocates XML Schema for data-centric XML apps (developerWorks, June 2001).
- IBM's DB2 XML Extender page gives a basic overview of how DB2 works with XML, and includes links to a detailed white paper on querying with XML (viewable as a PDF file) and to DB2 XML Extender downloads.
- Want to find out more about working with XML and IBM's DB2 and WebSphere Application Server? The IBM Redbook Integrating XML with DB2 XML Extender and DB2 Text Extender shows how to use XML technology efficiently in business applications, and explains how to integrate it with DB2 Universal Database, DB2 XML Extender and Text Extender, and WebSphere Application Server. This resource can help developers to set up the environment and to create and process XML documents that can be stored and recovered using SQL.
- Find other articles in Kevin William's XML for Data column.
- Find a wealth of XML-related content in the developerWorks XML zone.
- Check out professional XML developer certification, part of IBM's professional developer program. Also see XML@Whiz, a third-party certification training site that has a $75 tutorial with mock certification test, plus a free trial version (with fewer tests and questions).
Kevin Williams is the CTO of Blue Oxide Technologies, LLC, a company that designs software tools that help companies take advantage of the service-oriented Internet. Visit their Web site at http://www.blueoxide.com. He can be reached for comment at kevin@blueoxide.com.
Comments (Undergoing maintenance)





