Make your own free website on Tripod.com


Comparing XSLT 1.0 and XSLT 2.0

In this article, I shall compare the XSLT 1.0 and XSLT 2.0 programming languages. I have seen the XSLT language evolving from version 1.0 to 2.0, therefore I find it quite interesting to compare the two language versions.

How old are these two languages, and a little bit of history?
XSLT 1.0 became the W3C Recommendation on 16 November 1999. So it's about 8 years that XSLT 1.0 has moved forward. XSLT 2.0 became the W3C Recommendation on 23 January 2007. So it's less than a year that XSLT 2.0 has been officially recognized. Although it's only after XSLT 2.0 became the W3C Recommendation, that the XSLT community has taken the 2.0 version seriously, but Michael Kay's Saxon product started supporting early drafts of XSLT 2.0, from as early as June 2004. A very nice and robust implementation of XSLT 2.0 by Saxon since long time has encouraged XSLT users to experiment and use the 2.0 language. As of today, Saxonica (from Michael Kay), Altova and Gestalt (from Colin Adams) have produced the XSLT 2.0 implementations. I personally find Saxon to be the best 2.0 implementation today, because it implements the standard to the fullest, it's fast, and has a very good support.
Among the XSLT 1.0 processors, I have found Xalan-J, Saxon and MSXML to be very good. Although, there are other 1.0 processors also, but I haven't had a chance to use them.

Changes at a glance
Following are the major differences between XSLT 1.0 and XSLT 2.0.

1) Schema Awareness: XSLT 2.0 defines two (conformance) levels for the language. The XSLT 2.0 processor can be a basic XSLT processor, or it can be a Schema aware processor. The Schema aware facility was not there in the XSLT 1.0 language. The stylesheets being schema aware is a very powerful concept. A Schema aware stylesheet can import an external XML Schema, and the types available in the external schema become available to the stylesheet. This allows the type system of XSLT to be infinitely extensible. For example, we can then use user-defined types in the XPath expressions and template match patterns. A Schema aware XSLT 2.0 processor can also validate input and output trees, and also validate temporary trees (which might be used during the course of the transformation). Apart from these easily visible functional benefits of the Schema aware XSLT processor, Schema awareness defines a novel mechanism of XSLT solution development. From the overall solution development perspective, Schema awareness contributes to more robust stylesheets, powerful debugging, easily maintainable stylesheets, and the solution is more close to the business problem.

2. Enhanced Type System: The type system of XSLT 2.0 is lot more enhanced than XSLT 1.0. All the W3C Schema types are available in XSLT 2.0. Apart from this, as explained in point 1., the type system of XSLT can be extended with user-defined types from an external schema. XSLT 1.0 had a very restricted type system. It only defined 5 data types: String, number, boolean, node-set and result tree fragment. So one could imagine the power and flexibility that XSLT 2.0 provides to the programmer with a very extensive type system. The type system of XSLT 2.0 being that of W3C Schema, is a boon, as can be imagined.

3. Enhanced built in functions and operators: The function library of XSLT 2.0 has been enhanced considerably to meet varied requirements. A whole new W3C Recommendation has been published to define the XPath 2.0 functions and operators. The specification can be accessed at, http://www.w3.org/TR/xpath-functions/. The functions and operators defined in this Recommendation are related to XPath 2.0. Apart from this, XSLT 2.0 defines many new functions as compared to XSLT 1.0.

4. Enhanced XSLT Instruction Set: Many XSLT instructions have been enhanced with new options, to meet user requirements. New instructions have also been defined to accomplish complex tasks. Please refer the XSLT 2.0 specification for details.

5. Regular Expression support: Many XSLT 2.0 instructions can now operate on regular expressions. For e.g., matches, replace and tokenize functions make use of regular expressions, which make these functions quite lot more powerful.

6. Enhanced Data Model: The XPath 2.0 data model is lot more enhanced and quite different than XPath 1.0. A whole new W3C Recommendation has been published in this regard, http://www.w3.org/TR/xpath-datamodel/. The data model defines what language instructions can produce, and what will be the structure of input and output trees. A language is closed with respect to a data model if the value of every expression in the language is guaranteed to be in the data model. XSLT 2.0, XQuery 1.0, and XPath 2.0 are all closed with respect to the data model. [quoted from Data Model spec].
One of the major changes in XPath data model is, that XSLT instructions now don't write to the result tree. But instead, XSLT instructions now produce sequence of nodes and atomic values. This is largely a change in terminology (and do not make any difference to the end user as such). This allows the XSLT 2.0 stylesheet to manipulate arbitrary sequences of nodes and atomic values, rather than tied to a tree based data model. 

7. Multiple Result Trees: XSLT 1.0 could only output a single result tree. XSLT 2.0 allows to create more than one result tree. A final result tree can be created explicitly, by evaluating an xsl:result-document instruction. As explained in 2.4 Executing a Transformation, a final result tree is also created implicitly if no xsl:result-document instruction is evaluated, or if the result of evaluating the initial template is a non-empty sequence. [quoted from XSLT 2.0 spec].

8. xsl:function instruction: XSLT 2.0 supports a new instruction, called xsl:function which allows user-defined functions to be used in XPath expressions. This is a very useful enhancement in the 2.0 language.

9. Allowing templates and functions to accept and return typed values: The XSLT templates and functions can now accept and return typed values. This allows the templates and functions to be more robust, and respond better to the data input.

10. Enhanced error handling: XSLT 2.0 defines various conditions when errors can occur (during compilation of the stylesheet - static errors, and at run time - dynamic errors). For various errors conditions, error codes have been defined, and the XSLT 2.0 processor is required to support these error conditions and codes.

11. Enhanced Grouping facilities: With the introduction of xsl:for-each-group instruction in XSLT 2.0, grouping of XML data is now much simpler.

I have tried to convey the feel of differences between XSLT 1.0 and XSLT 2.0. To know in detail about XSLT 2.0 and the related specifications, I encourage to read from the specifications themselves (mentioned in References).

My overall feeling is, XSLT 2.0 is a big step forward, and should be adopted as widely as possible.

References:
1) http://www.w3.org/TR/xslt20/
2) http://www.w3.org/TR/xpath20/
3) http://www.w3.org/TR/xpath-functions/
4) http://www.w3.org/TR/xpath-datamodel/