Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Peek Into the Future of XSLT 2.0 : Page 3

XSLT 2.0 provides powerful new features that XSLT 1.0 was missing. Although XSLT 2.0 is still under development, Mike Kay's Saxon 7 implementation lets you experiment with the new capabilities now.




Application Security Testing: An Integral Part of DevOps

XML Schema Support Is Controversial
One of the most interesting—if controversial—aspects to XSLT 2.0 is its support of XML Schema. The controversy is based on a concern among some XML developers that XML Schema is becoming too entrenched into the core of the language. But if you yearn for better data typing in XML you'll appreciate the ability to use it, and if you loathe the concept entirely, you can safely ignore it as long as your requirements don't dictate its use.

XPath 2.0 provides access to the XML Schema data types as defined by the W3C. To take advantage of this capability in XSLT, you use the xsl:import-schema element, which is a top level element new to XSLT 2.0, like this:

<xsl:import-schema namespace = "http://www.w3.org/2001/ XMLSchema-instance" schema-location = "product.xsd" />

This gives you access to the data type model provided in XML Schema. You'll need it if you want to do any explicit type casting, which is beyond the scope of this article but allows you to specify how you want data results returned to you (as decimal types, date types, date-time types, etc).

New Features Fulfill Developers' Wishes
Multiple Document Output Capability
One feature for which XSLT developers have yearned is multiple output capability. Some processors already support multiple file output via extensions. Xalan, in particular, has a rather substantial pipelining process. This has proven useful for generating multiple Web site pages from one stylesheet. XSLT 2.0 now supports it natively through the xsl:result-document element. An example of this is shown in Listing 2 (the file output.xsl in the sample code). The stylesheet processor generates a result file for each instance of the product element, and also generates a link to each resulting file.

XSLT 2.0 Simplifies Syntax
XSLT has vastly cut down on its verbosity with Version 2.0. A perfect example is grouping, which can be a Herculean task in XSLT 1.0. Steve Muench of Oracle managed to piece together a brilliant mechanism for creating distinct groupings in XSLT 1.0, but making it work can sometimes make even the most grizzled XSLT veteran eat analgesics by the bucketful. I'll walk you through a grouping example a bit later in this article, and you'll see first-hand how much easier this common task has become.

Regular Expressions Improve String-Handling
The introduction of regular expressions is another improvement—and is something many XSLT developers have longed for. Most people find XSLT 1.0's string manipulation capabilities wanting. Regular expressions change this completely.

More Robust Expressions
As nice as the simplified syntax is, even better is how robust XPath expressions have become. Consider the following statement, which harkens back to the kind of programming most of us are used to:

<xsl:value-of select="for $x in following-sibling::quantity return $x"/>

This statement declares a variable, stores it, and lets you drill down (or up) from the result of that variable—all within the same expression. The statement, in effect, says, "Declare the variable x, store the value resulting from evaluating the quantity element that is a following-sibling on the path from the context node, and then return the result." You can do still more, such as access a child element of the result, by doing this instead:

<xsl:value-of select="for $x in following-sibling::quantity return $x/childElement"/>

The child elements are accessible in the preceding code because you have access to the sequence results from evaluating the variable expression.

Don't get too excited, though, variables are still static. Nevertheless, there is a significant change in XSLT's verbosity, and people who've shunned it for that reason will probably be taking a second look. Keep your mind trained on the preceding example, because later in this article you'll see how you can use similar expressions to optimize your code.

The document() Function Migrates to XPath
The document() function has proven to be so useful in XSLT that it has found its way into the XPath 2.0 specification. You can now use it as part of a location path within XPath. If you've used this function in XSLT you understand the basics behind how it works.

The document() function in XSLT gives you access to all the nodes in a tree of an external document. Consider this variable:

<xsl:variable name="doc" select="document('foo.xml')" />

The variable stores the entire tree from the XML document in foo.xml. Then, you can get at the rest of the tree. Assuming the root element is <bar>, you could get the text value of that element by doing this:

<xsl:value-of select="$doc/bar" />

In fact, XPath has so many new functions added to it that the W3C split the specification. Functions now have their own standards document, currently in Working Draft stage.

Improved Comment Capability
You can still use comments the old fashioned way, by using the <!-- and --> delimiters, but now you can embed them into expressions, too, using the delimiters {-- and --} to start and end the comment.

<xsl:apply-templates select="{-- This is a comment within an expression. This is new for XPath 2.0 --} ../data[@name=current()/@att]"/>

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date