 |
 |
 |
Introduction
New in XSLT/XPath 2.0
Benefits of Using XSLT/XPath 2.0 Now
As with any 2.0 release, XSLT 2.0 and XPath 2.0 supply functionality missing in the 1.0 specifications. Release 2.0 also adds powerful new features to the languages, extending them in important ways. They’re more concise, more efficient, more flexible, and significantly more powerful than their 1.0 predecessors.
XPath 2.0 is the result of the combined efforts of the W3C XSL and XQuery Working Groups, and, in fact, XQuery is an extension of XPath 2.0.
XPath 2.0 is used with both XSLT 2.0 and XQuery 1.0. XSLT continues to be the primary choice for transforming XML data, while XQuery is slated to become the standard for querying XML documents. Though both XQuery and XSLT 2.0 use XPath 2.0 as a path language, the XQuery extensions to XPath 2.0 are not of practical relevance to XSLT developers. XPath 2.0 and XSLT 2.0 are inseparably linked and are ready for use today.
Altova is the first in the industry to offer native, production-grade implementations of the powerful new XSLT 2.0 and XPath 2.0 specifications in all of its XML development tools as well as in its content editing product for business users. Now you can use the industry’s most trusted XML product line to work with XSLT 2.0 and XPath 2.0 in a standards-based way to truly harness the power of the XML set of standards. Please note that the material in this document is intended for developers familiar with XPath and XSLT. For more information on the business and development benefits of XSLT 2.0, read the InfoTrends/CAP Ventures Analysis Report, "XSLT 2.0: Understanding the Development and Business Benefits."
It should be noted that, though XSLT 2.0 and XPath 2.0 are designed to be backwards compatible with their 1.0 versions, using the new features of XPath 2.0 with XSLT 1.0 is generally not supported.
The most significant improvements addressed by XSLT 2.0 and XPath 2.0 are listed here and defined further below. - Type system based on sequences of nodes
- Support for XML Schema datatypes
- Elimination of Result Tree Fragments (RTFs)
- Support for node grouping
- Aggregation functions
- "For" loops
- Support for conditional expressions
- Support for regular expressions
- Multiple output documents
- XHTML output
- User-defined functions
- New functions and operators
Sequences of Nodes
XPath 1.0 expressions returned a node set, which is an unordered collection of items. In XPath 2.0, every XPath expression returns a sequence, which is an ordered grouping of atomic values or nodes. Sequences are shallow, i.e., not nested, and they may contain duplicate items, another improvement over XPath 1.0. What is more, you can create a sequence yourself in XPath 2.0. A simple expression can create a collection of values, for instance:
( 'this', 'that', 'the other' ), ( 'this', 'these' )
This expression creates two sequences and joins them into one, a modest goal that is next to impossible in XPath 1.0.
Support for Schema Datatypes
Datatyping becomes much stronger with XPath 2.0. Whereas XPath 1.0 processes everything as a string, XPath 2.0 combines the power of XML and XML Schema with support for XML Schema datatypes. XPath 1.0 supported four expression types, but with support for XML Schema primitive types in 2.0, you immediately get 19 more simple types to choose from and the ability to further refine those types to suit your application. Try this simple expression in the XMLSpy XPath Evaluator:
xs:token( "city" ) instance of xs:string
You will see that the answer is “true” (labeled as an xs:boolean) because an xs:token is a type derived from xs:string in the hierarchy of XML Schema datatypes.
Conversion of RTFs to Node Sets
Result Tree Fragments (RTFs) were a major limitation of XSLT 1.0 that required verbose work-arounds or proprietary extensions for processing items in temporary trees. In XSLT 2.0, RTFs are eliminated. xsl:variable returns a true node set (sequence), and you can work with it just as you would with any other sequence. Moreover, as this quick example demonstrates, you can navigate variables containing temporary trees just as if they contained a document root.
<xsl:template match="/">
<xsl:variable name="temp">
<xsl:element name="a">
<xsl:element name="b">first</xsl:element>
<xsl:element name="b">second</xsl:element>
</xsl:element>
</xsl:variable>
<result>
<xsl:value-of select="$temp/b[2]"/>
</result>
</xsl:template>
The result of this transformation is a single node with the value “second”. As you can see, unlike in XPath 1.0, the variable “$temp” is a completely traversable tree of nodes.
Node Grouping
XSLT 2.0 includes built-in support for grouping, which was an extremely difficult and error-prone task in XSLT 1.0. XSLT 2.0 introduces xsl:for-each-group, which allows you to group a collection of nodes, for example, to group a list of addresses by country. The xsl:for-each-group element supports three attributes: group-by, group-adjacent, and group-starting-with, and the current-group() function allows you to refer to a group after defining it. Now it’s easy to group related data for producing different views of XML data.
As a simplified example, consider a troubleshooting table containing sets of symptoms, causes and solutions:
<table>
<title>troubleshooting</title>
<tr>
<td name="symptom">wet</td>
<td name="symptom">muddy</td>
<td name="cause">raining</td>
<td name="solution">get hat</td>
<td name="solution">wear boots</td>
</tr>
<tr>
<td name="symptom">hungry</td>
<td name="cause">lunch time</td>
<td name="solution">eat lunch</td>
</tr>
<table>
Perhaps you want to generate an XML document that groups the items of each category so that each category becomes the value of one element, and all of the values for that category become part of a comma separated list in a second element. Using XSLT 2.0 you could very quickly write a template like:
<xsl:template match="/">
<sets>
<xsl:for-each select="table/tr">
<xsl:for-each-group select="td" group-by="@name">
<a>
<xsl:value-of select="@name"/>
</a>
<b>
<xsl:value-of select="current-group()"
separator=", "/>
</b>
</xsl:for-each-group>
</xsl:for-each>
</sets>
</xsl:template>
To get output like:
<sets>
<a>cause</a>
<b>raining</b>
<a>solution</a>
<b>get hat, wear boots</b>
<a>symptom</a>
<b>wet, muddy</b>
<a>cause</a>
<b>lunch time</b>
<a>solution</a>
<b>eat lunch</b>
<a>symptom</a>
<b>hungry</b>
</sets>
Aggregation
The XPath 2.0 aggregation functions min ( ), max ( ), avg ( ), etc., work hand-in-hand with the grouping support in XSLT 2.0, allowing functions to be applied to data in groups. These two features further extend the power of XML for representing structured data. (For an example of these functions in action see the “Multiple Output Documents” code example below.) "For" loops, conditional expressions, regular expressions
XPath 1.0 has been criticized for being cryptic and difficult to learn. In contrast, extensions such as support for “for” loops, conditional expressions, and regular expressions make XPath 2.0 a true programming language. Programmers will find these common programming constructs familiar, so the XSLT/XPath 2.0 learning curve is shallow, and writing code is faster and more productive.
The ability to create a variable that takes the value of each member of a sequence consecutively in a "for" loop will be refreshing for most XPath users. As an example, suppose you want to know if there are “many” or “few” items in the table of data used above. The following XPath would give you the answer:
for $row in //tr
return
if ( count( $row/td ) > 3 )
then "many"
else "few"
Multiple Output Documents
In XSLT 1.0, an XSLT processor run could only produce one output document. With XSLT 2.0 it’s possible to produce multiple documents from a single run through a stylesheet, removing the need for proprietary extensions or separate scripting or batch processing in other languages to accomplish the same thing. In addition, XSLT 2.0 improves support for multiple input documents with support for non-XML input files and more.
If you add an “incidents” attribute to each <td> in the troubleshooting data given above, you can then modify your template to provide both a details and a summary document by using the xsl:result-document element:
<xsl:template match="/">
<xsl:result-document href="output1.xml">
<sets>
<xsl:for-each select="table/tr">
<xsl:for-each-group select="td" group-by="@name">
<a><xsl:value-of select="@name"/> (<xsl:value-of
select="count ( current-group() )"/>) </a>
<b>
<xsl:value-of select="current-group()" separator=", "/>
<xsl:if test="count( current-group() ) > 1">
(average: <xsl:value-of select="avg
( current-group()/@incidents ) "/>)
</xsl:if>
</b>
</xsl:for-each-group>
</xsl:for-each>
</sets>
</xsl:result-document>
<xsl:result-document href="output2.xml">
<summary>
<xsl:for-each select="table/tr">
<xsl:for-each-group select="td" group-by="@name">
<row>
<xsl:value-of select="@name"/> has <xsl:value-of
select="count ( current-group() )"/>
<xsl:choose>
<xsl:when test="count( current-group() ) > 1"> items,
each occurring an average of <xsl:value-of select="avg
( current-group()/@incidents ) "/> times.</xsl:when>
<xsl:otherwise> item that occurs <xsl:value-of select="avg
( current-group()/@incidents ) "/> times. </xsl:otherwise>
</xsl:choose>
</row>
</xsl:for-each-group>
</xsl:for-each>
</summary>
</xsl:result-document>
</xsl:template>
When run, your output files might look like:
<sets>
<a>cause (1) </a>
<b>raining</b>
<a>solution (5) </a>
<b>get hat, wear boots, use windshield wipers, use headlights,
other (average: 2.2)</b>
<a>symptom (4) </a>
<b>wet, muddy, slippery, poor visibility (average: 2.75)</b>
<a>cause (1) </a>
<b>lunch time</b>
<a>solution (1) </a>
<b>eat lunch</b>
<a>symptom (1) </a>
<b>hungry</b>
</sets>
and
<summary>
<row>cause has 1 item that occurs 11 times. </row>
<row>solution has 5 items, each occurring an average of 2.2 times.</row>
<row>symptom has 4 items, each occurring an average of 2.75 times.</row>
<row>cause has 1 item that occurs 8 times. </row>
<row>solution has 1 item that occurs 8 times. </row>
<row>symptom has 1 item that occurs 8 times. </row>
</summary>
In addition to multiple document output, XSLT 2.0 improves support for multiple input documents with support for non-XML input files and more.
XHTML Output
XSLT 1.0 allows output in XML, HTML, and text, and version 2.0 adds support for XHTML output. User-defined Functions
The xsl:function declaration in XSLT 2.0 allows you to define XSLT functions to be used in XPath 2.0 expressions. xsl:function declares the name, parameters, and implementation of a user-defined function used within XPath expressions in a given stylesheet.
In order to be able to write a template including this XSLT element:
<xsl:value-of select=" a:nextNumber( 1 )"/>
You would need to use xsl:function to define a:nextNumber as an custom XPath function. Once defined, a:nextNumber would be available to XPath expressions anywhere within the stylesheet containing the definition.
<xsl:function name="a:nextNumber">
<xsl:param name="number" as="xs:integer"/>
<xsl:choose>
<xsl:when test="$number = 1">
<xsl:sequence select="string('Two')"/>
</xsl:when>
<xsl:when test="$number = 2">
<xsl:sequence select="string('Three')"/>
</xsl:when>
</xsl:choose>
</xsl:function>
The result of calling this function would be the word “Two” being added to a text node in the output tree.
New Functions and Operators
XPath 2.0 supports a breathtaking number of new functions and operators, a few of which include: - every – checks if every (as opposed to any) node satisfies a criterion
- intersect – easier way to check if a node is contained in a node-set
- except – selects all nodes of a node-set except XYZ
- And many more
A quick example of the except operator will give a sense of the greater potential for simplicity and elegance in XPath 2.0 solutions. Again using the troubleshooting data file given above, try applying the following statement using the XMLSpy XPath Evaluator:
//td except //td[@name = 'cause']
The result is a list of all the symptoms and solutions in the file, with no extra effort required.
Please note: The list of items above is not exhaustive – XSLT 2.0 and XPath 2.0 provide countless new features and improvements. A complete list of the major new features of XSLT 2.0 is available in the XSL Transformations (XSLT) Version 2.0 W3C Working Draft. XPath 2.0 features are defined in the XML Path Language (XPath) 2.0 W3C Working Draft.
The XSLT 2.0 Working Draft has remained fairly stable since late 2003, which indicates that it’s reached maturity and will not likely change significantly between now and the final recommendation.
Though XPath 2.0 is also used with XQuery, the required backend database support for XQuery is not ready for prime time. In contrast, XSLT 2.0 doesn’t require support from back-end technologies and is ready to be used with XPath 2.0 today. In fact, Altova provides commercial-grade support today for both XSLT 2.0 and XPath 2.0 in its award-winning XSLT processor, which, in addition to being included in all of its products as described below, is available under a free license. The Altova XSLT 2.0 processor will allow you to move from just experimenting with XSLT/XPath 2.0 to implementing it in real world applications. Finally, in addition to the other benefits of the 2.0 specifications, learning XPath 2.0 today with XSLT 2.0 will give you the know-how you need for future XQuery development.
The development and business advantages of XSLT/XPath 2.0 are too significant to ignore. XSLT 2.0 is designed to be backwards compatible with XSLT 1.0, so, with a few exceptions, you can harness the power of XSLT/XPath 2.0 in your existing XSLT stylesheets with zero or minor modifications.
In addition, these new specifications have a very shallow learning curve. You can leverage your existing knowledge of XSLT, XPath, and XML Schema, which is the foundation for the new XSLT/XPath 2.0 datatype extensions. With version 2.0 XPath becomes a true programming language, and the extensions to both languages draw on well-known, established programming language constructs, so you’ll immediately be familiar with them.
Version 2.0 extends XSLT and XPath with important functions that were missing in 1.0. In the past, these functions were added using proprietary extensions, but this created incompatibilities and locked developers into using a specific XSLT processor. The functions covered by the XSLT/XPath 2.0 standards remove the need for proprietary extensions. This increases compatibility significantly. Now stylesheets are truly portable, and the same stylesheet can be used with multiple processors on multiple platforms. In this way, XSLT 2.0 promises to end the “XSLT processor war” the same way XHTML ended the “browser war” a few years ago.
Overall, the new features in version 2.0 are designed to make development simpler, more efficient, and therefore more productive, allowing you to produce higher-quality applications in less time.
Industry’s First Production-grade Implementation
Perhaps the most compelling reason to use XSLT 2.0 and XPath 2.0 today is that a production-ready, standards-conformant implementation is available from Altova today. Altova XMLSpy is the standard XML development environment, and the Altova suite of XML development tools is the choice of over 2 million users worldwide.
Altova’s commercial-grade implementation is based on its award-winning XSLT processor, which was recently named “Best XSLT Processor” by XML Journal. Altova support covers every aspect of XSLT/XPath 2.0 development, from visual stylesheet design in StyleVision® 2008, to data mapping in MapForce® 2008, to editing, debugging, and testing in XMLSpy® 2008.
For more information about Altova support for XSLT 2.0 and XPath 2.0, visit the following pages in the Altova Developer Portal:
XSLT
XPath
To start working with XSLT 2.0 and XPath 2.0 now, download a free 30-day trial of the Altova® MissionKit™ 2008. Get up to 8 software tools for the price of 2 with the Altova® MissionKit™ 2008! The MissionKit bundles Altova's intelligent application development and data management tools to meet the needs of software architects and XML developers.
|
 |
 |
 |