简体   繁体   English

将XSLT 1.0升级到XSLT 2.0

[英]Upgrading XSLT 1.0 to XSLT 2.0

What is involved in upgrading from XSLT 1.0 to 2.0? 从XSLT 1.0升级到2.0涉及什么?
1 - What are the possible reasons for upgrading? 1 - 升级的可能原因是什么?
2 - What are the possible reasons for NOT upgrading? 2 - 不升级的可能原因是什么?
3 - And finally, what are the steps to upgrading? 3 - 最后,升级的步骤是什么?

I'm hoping for an executive summary--the short version :) 我希望有一个执行摘要 - 简短的版本:)

What is involved in upgrading from XSLT 1.0 to 2.0? 从XSLT 1.0升级到2.0涉及什么?

1 - What are the possible reasons for upgrading? 1 - 升级的可能原因是什么?

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). 如果您是XSLT程序员,您将从更方便和富有表现力的XSLT 2.0语言+ XPath 2.0和新的XDM(XPath数据模型)中受益。

You may want to watch this XSLT 2.0 Pluralsight course to get firm and systematic understanding of the power of XSLT 2.0 . 您可能希望观看此XSLT 2.0 Pluralsight课程,以便对XSLT 2.0的强大功能有一个坚定而系统的了解

You have: 你有:

  • Strong typing and all XSD types available. 强类型和所有XSD类型可用。

  • 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. 在XPath 1.0中没有任何对应物(简单地丢失)的XPath 2.0序列类型。

  • The ability to define and write functions in pure XSLT -- the xsl:function instruction. 在纯XSLT中定义和编写函数的能力 - xsl:function指令。

  • Range variables in XPath expressions (the for clause). XPath表达式中的范围变量( for子句)。

  • Much better and more powerful string processing -- XPath 2.0 supports regular expressions in its tokenize() , matches() and replace() functions. 更好更强大的字符串处理 - XPath 2.0支持其tokenize()matches()replace()函数中的正则表达式。

  • 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. 更好,更强大的字符串处理 - XSLT 2.0支持正则表达式 - xsl:analyze-stringxsl:matching-substringxsl:non-matching-substring新的XSLT指令。

  • More convenient, powerful and expressive grouping: the xsl:for-each-group instruction. 更方便,更强大和富有表现力的分组: xsl:for-each-group指令。

  • A lot of new, very powerful XPath 2.0 functions -- such as the functions on date, time and duration, just to name a few. 许多新的,非常强大的XPath 2.0功能 - 例如日期,时间和持续时间的功能,仅举几例。

  • The new XPath operators intersect , except , is , >> , << , some , every , instance of , castable as , ..., etc. 新的XPath运算符intersectexceptis>><<someeveryinstance ofcastable as ,......等等

  • The general XPath operators > , < , etc. now work on any ordered value type (not only on numbers as in XPath 1.0). 一般的XPath运算符><等现在可以处理任何有序的值类型(不仅仅是在XPath 1.0中的数字)。

  • New, safer value comparison operators: lt , le , eq , gt , ge , ne . 新的,更安全的价值比较运算符: ltleeqgtgene

  • The XPath 2.0 to operator, allowing to have xsl:for-each select="1 to $N" XPath 2.0 to运算符,允许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. 这些以及许多其他改进/新功能显着提高了任何XSLT程序员的工作效率,这使得XSLT 2.0开发能够在使用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. 对我来说,这种强大的类型安全性是使用XSLT 2.0的最大优势。

2 - What are the possible reasons for NOT upgrading? 2 - 不升级的可能原因是什么?

  • 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. 通常可以,合理且经济高效地保留现有的旧版XSLT 1.0应用程序,并继续将它们与XSLT 1.0一起使用,同时仅使用XSLT 2.0开发新的应用程序。

  • Your management + any other non-technical reasons. 您的管理层+任何其他非技术原因。

  • Having a lot of legacy XSLT 1.0 applications written in a poor style (eg using DOE or extension functions that now need to be re-written and the code refactored). 有很多遗留的XSLT 1.0应用程序以糟糕的方式编写(例如,使用现在需要重写的DOE或扩展函数并重构代码)。

  • Not having available an XSLT 2.0 processor. 没有XSLT 2.0处理器。

3 - And finally, what are the steps to upgrading? 3 - 最后,升级的步骤是什么?

  • Change the version attribute of the xsl:stylesheet or xsl:transform element from "1.0" to "2.0" . xsl:stylesheetxsl:transform元素的version属性从"1.0"更改为"2.0"

  • Remove any xxx:node-set() functions. 删除任何xxx:node-set()函数。

  • Remove any DOE. 删除任何DOE。

  • Be ready for the surprise that xsl:value-of now outputs not just the first, but all items of a sequence. 准备好让xsl:value-of现在输出的不仅仅是第一个,而是序列的所有项目。

  • Try to use the new xsl:sequence instruction as much as possible -- use it to replace any xsl:copy-of instructions; 尝试尽可能多地使用新的xsl:sequence指令 - 用它来替换任何xsl:copy-of指令; use it instead of xsl:value-of any time when the type of the output isn't string or text node. 当输出的类型不是字符串或文本节点时xsl:value-of任何时候使用它代替xsl:value-of

  • 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 . (可选)决定将哪些命名模板重写为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. 确定您是否仍需要旧版本中使用的某些扩展函数,或者您可以使用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. 项目中开始使用所有XSLT 2.0 / XPath 2.0功能要清晰得多。

Dimitre's answer is very comprehensive and 100% accurate (as always) but there is one point I would add. Dimitre的答案非常全面,100%准确(一如既往),但有一点我会补充。 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". 升级到2.0处理器时,您可以选择将版本属性设置为“1.0”并以“向后兼容模式”运行,或将版本属性更改为“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. 我的建议是,如果你对样式表有一套很好的测试,请选择:set version =“2.0”,运行测试,如果有任何问题,请修复它们。 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. 但是如果你没有一套好的测试并且担心工作负载的可靠性,那么保留version =“1.0”是一种风险较低的方法:处理器将模拟XSLT 1.0的所有怪癖,例如: xsl:value-of忽略除第一项之外的所有项,以及用于将数字与字符串进行比较的奇怪规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM