Upgrading XSLT 1.0 to XSLT 2.0

53

29

What is involved in upgrading from XSLT 1.0 to 2.0?
1 - What are the possible reasons for upgrading?
2 - What are the possible reasons for NOT upgrading?
3 - And finally, what are the steps to upgrading?

I'm hoping for an executive summary--the short version :)

Witman

Posted 2012-02-22T00:20:11.417

Reputation: 968

Answers

102

What is involved in upgrading from XSLT 1.0 to 2.0?

1 - What are the possible reasons for upgrading?

If you are an XSLT programmer you'll benefit largely from the more convenient and expressive XSLT 2.0 language + XPath 2.0 and the new XDM (XPath Data Model).

You may want to watch this XSLT 2.0 Pluralsight course to get firm and systematic understanding of the power of XSLT 2.0.

You have:

  • Strong typing and all XSD types available.

  • The ability to define your own (schema) types.

  • the XPath 2.0 sequence type that doesn't have any counterpart (simply is missing) in XPath 1.0.

  • The ability to define and write functions in pure XSLT -- the xsl:function instruction.

  • Range variables in XPath expressions (the for clause).

  • Much better and more powerful string processing -- XPath 2.0 supports regular expressions in its tokenize(), matches() and replace() functions.

  • Much better and more powerful string processing -- XSLT 2.0 support for regular expressions -- the xsl:analyze-string, xsl:matching-substring and xsl:non-matching-substring new XSLT instructions.

  • More convenient, powerful and expressive grouping: the xsl:for-each-group instruction.

  • A lot of new, very powerful XPath 2.0 functions -- such as the functions on date, time and duration, just to name a few.

  • The new XPath operators intersect, except, is, >>, <<, some, every, instance of, castable as, ..., etc.

  • The general XPath operators >, <, etc. now work on any ordered value type (not only on numbers as in XPath 1.0).

  • New, safer value comparison operators: lt, le, eq, gt, ge, ne.

  • The XPath 2.0 to operator, allowing to have xsl:for-each select="1 to $N"

These, and many other improvements/new features significantly increase the productivity of any XSLT programmer, which allows XSLT 2.0 development to be finished in a small fraction of the time necessary for developing the same modules with XSLT 1.0.

Strong typing allows many errors to be caught at compile time and to be corrected immediately. For me this strong type-safety is the biggest advantage of using XSLT 2.0.

2 - What are the possible reasons for NOT upgrading?

  • It is often possible, reasonable and cost-efficient to leave existing, legacy XSLT 1.0 applications untouched and to continue using them with XSLT 1.0, while at the same time developing only new applications using XSLT 2.0.

  • Your management + any other non-technical reasons.

  • Having a lot of legacy XSLT 1.0 applications written in a poor style (e.g. using DOE or extension functions that now need to be re-written and the code refactored).

  • Not having available an XSLT 2.0 processor.

3 - And finally, what are the steps to upgrading?

  • Change the version attribute of the xsl:stylesheet or xsl:transform element from "1.0" to "2.0".

  • Remove any xxx:node-set() functions.

  • Remove any DOE.

  • Be ready for the surprise that xsl:value-of now outputs not just the first, but all items of a sequence.

  • Try to use the new xsl:sequence instruction as much as possible -- use it to replace any xsl:copy-of instructions; use it instead of xsl:value-of any time when the type of the output isn't string or text node.

  • Test extensively.

  • When the testing has verified that the code works as expected, start refactoring (if deemed necessary). It is a good idea to declare types for any variables, parameters, templates and functions. Doing so may reveal new, hidden errors and fixing them increases the quality of your code.

  • Optionally, decide which named templates to rewrite as xsl:function.

  • Decide if you still need some extension functions that are used in the old version, or you can rewrite them easily using the new, powerful capabilities of XSLT.

Final remarks: Not all of the above steps are necessary and one can stop and declare the migration successful on zero bug testing results. It is much cleaner to start using all XSLT 2.0/XPath 2.0 features in new projects.

Dimitre Novatchev

Posted 2012-02-22T00:20:11.417

Reputation: 209 189

1Thanks for the great overview, Dimitre (+1). From what I can tell, one must choose between a version 1 or version 2 processor--can't have both and route documents to the appropriate one (please correct me if I'm wrong). With significant corporate site (that all work must fit into) on ASP.NET & jQuery, is there an XSLT 2.0 processor that would be a likely fit? – Witman – 2012-02-22T03:21:45.640

1@Witman: You are welcome. The routing to different version processors is easy to do and shouldn't be a problem. XSLT 2.0 can operate in backwards compatibility mode but this isn't exactly the same as processing by an XSLT 1.0 processor. At present there are two XSLT 2.0 processors that are fit for .NET -- Saxon.NET and XmlPrime (XQSharp). Saxon also has an XSLT 2.0 processor version that can be used client-side -- inside a browser -- this is Saxon CE. I think that it must be your own decision -- you should try both Saxon.NET (and CE) and XmlPrime and then make an informed choice. – Dimitre Novatchev – 2012-02-22T03:47:58.240

1Thanks for the clarification on routing to different processors and processor suggestions. (+1) – Witman – 2012-02-22T05:14:45.977

1I would "favorite" this answer if I could. :-) A very useful collection. Have you published an article along these lines? – LarsH – 2013-04-29T18:21:35.923

1P.S. I wish new-in-2.0 instructions like &lt;xsl:next-match&gt; and &lt;xsl:apply-imports&gt; were more widely publicized. Not that they're used that much, but I had a need for &lt;xsl:next-match&gt; this morning, couldn't remember what it was called, and had to browse through Jeni Tennison's powerpoint slides in order to find it. Not that you want to clutter a highlights list with every useful new instruction... – LarsH – 2013-04-29T18:26:14.877

@LarsH, No, I haven't published a blog post -- I have always thought that this is something that people must know and that they know. – Dimitre Novatchev – 2013-04-29T20:17:36.437

@Lars, To add to this, what is even more important to be publisized now is the new capabilities of XSLT 3.0 and XPath 3.0. – Dimitre Novatchev – 2013-04-29T20:18:57.100

1Dimitre, please don't underestimate the value of publishing the above. It's amazing how much people who use XSLT don't know about XSLT (including myself, and I've studied it a lot more than most who use it). Without downplaying the importance of 3.0, I think it would be very helpful to post some of these pointers about 2.0, since a lot more people are using 2.0 right now. – LarsH – 2013-04-29T20:39:40.353

1E.g. many people (including myself at one time, and I would expect many others) use XSLT 2.0 because of the advantages in grouping, and in multistage processing (applying templates to the output of other templates, without the unportable nodeset extension), yet do not know all the caveats of your part 3, in particular, "the surprise that xsl:value-of now outputs not just the first, but all items of a sequence". – LarsH – 2013-04-29T20:44:18.947

2

DOE stands for disable-output-escaping. http://www.dpawson.co.uk/xsl/sect2/N2215.html

– Gavilan Comun – 2015-05-06T10:10:11.263

Another issue I noticed while trying to upgrade was that Saxon complains about null/empty elements. You need to add some sort of null check for every null elements before you could do processing. – decent guy – 2015-12-01T00:29:02.927

1Excellent answer. Aside: you should indicate that you are the author of the pluralsight course. – Paulb – 2017-03-31T16:05:58.227

@Paulb Thank you for your appreciation. Having links to training courses is often frowned upon at SO, especially by the management-related roles. I think such links are added to the info page for the XSLT tags – Dimitre Novatchev – 2017-03-31T17:23:00.980

27

Dimitre's answer is very comprehensive and 100% accurate (as always) but there is one point I would add. When upgrading to a 2.0 processor, you have a choice of leaving the version attribute set to "1.0" and running in "backwards compatibility mode", or changing the version attribute to "2.0". People often ask which approach is recommended.

My advice is, if you have a good set of tests for your stylesheets, take the plunge: set version="2.0", run the tests, and if there are any problems, fix them. Usually the problems will be code that was never quite right in the first place and only worked by accident. But if you don't have a good set of tests and are concerned about the reliability of your workload, then leaving version="1.0" is a lower-risk approach: the processor will then emulate all the quirks of XSLT 1.0, such as xsl:value-of ignoring all but the first item, and the strange rules for comparing numbers with strings.

Michael Kay

Posted 2012-02-22T00:20:11.417

Reputation: 107 624