|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
XML Protocols What's New in XSLT 2.0
What's New in XSLT 2.0
By: Jeff Kenton
Mar. 28, 2003 12:00 AM
The XSLT version 1.0 language definition has been an official recommendation of the W3C since 1999. Its use has expanded dramatically in the past 18 months, for processing XML and XML/SOAP security policies and for generating HTML Web pages. Of course, nearly as soon as the language became official, people began proposing to change it. (Indeed, the original document has a page of suggested improvements for future versions.) These efforts began as a version 1.1 proposal, which was abandoned in favor of the current Working Draft (WD). We should see XSLT 2.0 become an official W3C Recommendation sometime this year.
Data Types Let's look at numbers. In XSLT 1.0, there was only a single variety of number, represented internally as a floating point double, and sometimes used explicitly as an integer. Now we'll have doubles, floats, various signed and unsigned integer precisions, and decimals. Decimals may be a new concept for some of you, unless you've been programming in COBOL recently. These are intended to provide exact representations of decimal fractions (e.g., dollars and cents) without the approximation caused by using floating point. So, we now have three different kinds of numeric constants we can create, instead of one:
Having all these new types will lead to greater flexibility, and, occasionally, things to watch out for. XSLT is now a strongly typed language, so it's possible that a parameter to a function you call will need to be an integer - if you pass a double instead, an error will result, unless you cast it properly. The new date and type data types are a great blessing to anyone who had to suffer through the ugly string manipulations required before. We now have duration, dateTime, date, time, gYearMonth, gYear, gMonthDay, gDay, and gMonth. What more could anyone want? And there will be a format-date() function available, although the details are not yet specified in the latest WD. Finally, we still have strings and Booleans. But what about nodesets? Technically, they have disappeared, replaced by sequences (lists) of nodes. In general, sequences are not necessarily in document order and may contain duplicates. However, all functions from XSLT 1.0 that returned nodesets in the past now return ordered sequences with duplicates removed. Old stylesheets will still work fine.
Path Expressions Book/(Chapter | Appendix)/Paragraph or document("a.xml")/id("ID01") There are two things to notice here. First, this applies to expressions, but not to patterns. So, you can use these in xsl:for-each or xsl:value-of statements, but not in the match patterns for templates. Match patterns have not changed, except that predicates in patterns can be XPath 2.0 expressions. The second thing to notice is that some things are legal that aren't especially useful: document("a.xml")/document("b.xml") or anything-on-the-left/$x Both of these ignore anything to the left of the last "/" and are equivalent to the nodeset that is the rightmost step. Hopefully, XSLT processors will produce a warning for these.
Conditionals and Looping
<xsl:value-of select=" This is entirely within XPath expressions, and is separate from xsl:if. (Old-timers may be reminded of Algol syntax here.) You can now generate sequences with for loops. Consider the following:
for $i in (0 to 9), $j in (1 to 10) This returns the sequence of numbers from 1 to 100. Of course, in this case, a simpler equivalent would use a range expression, (1 to 100), to generate the same sequence.
Grouping
<cars> Suppose you wanted to list the cars by make, or by color. The new xsl:for-each-group instruction makes life much easier:
<xsl:for-each-group select="cars/
You have a lot of flexibility with the xsl:for-each-group element. In addition to the required select element shown above, the following attributes are available:
Exactly one of these four attributes must appear. Within the xsl:for-each-group element, the new current-group() function gives access to the group members, and will make grouping much simpler than it was with XSLT 1.0.
Regular Expressions For simpler regular expression processing, XPath 2.0 has added three functions, fn:match(), fn:replace(), and fn:tokenize(). The first returns Boolean true or false, depending upon whether a string matches a given regular expression. The second replaces all substrings in the input string that match the regular expression with a replacement string, and returns the resulting string. The third uses a regular expression as a separator, and returns a sequence of substrings created by breaking the input at that separator. In addition, of course, XPath retains all its other string manipulation functions. This should go a long way to simplifying - and making more powerful - string handling in stylesheets.
User-Defined Functions
<xsl:function name="square" > returns the square of its input parameter. Children of xsl:function declarations may only be xsl:param, xsl:variable, xsl:message, or xsl:result. This might seem to limit what functions are capable of doing. However, there is no limit to what can appear inside the xsl:variable element, including xsl:call-template and xsl:apply-template, so you really have a great deal of flexibility.
Schemas With schema support, there is a new xsl:import-schema declaration. Every data type name that is not a built-in name must be defined in an imported schema. XPath expressions will be able to validate values against in-scope schemas, and will be able to use constructors and casts to imported data types.
Inputs and Outputs For output, the new xsl:result-document instruction provides for named and unnamed output trees. Combined with xsl:output declarations, this allows for multiple output documents. It is a feature that has been widely requested.
When Can I Try XSLT 2.0?
References Reader Feedback: Page 1 of 1
Your Feedback
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
||||||||||||||||||||||||||||||||||||||||||||||||||||