Sample Demonstrating XSLT 2.0 features
Date: 10-Jan-2005
Table of Contents
Introduction
Application Overview
Software Requirements
Terminology
Configuring and Running the Application
Sample Application Files
Additional References
Introduction
Prerequisite
To understand this sample the user is expected to have knowledge in the following area,
Technical Overview
During the past few years, XSLT (Extensible Style Sheet Language Transformation) has emerged as a widely used powerful language to translate and transform XML documents into other XML, text, HTML, etc. documents. XSLT is a tag based scripting language which helps in selecting specific tags/data within a XML document. However, XSLT in its current 1.0 version has limitations that can make writing stylesheets difficult and complex. XSLT 2.0 packs in itself a host of new features that will make the XSL scripts more simpler to implement and also make them vendor independent.
Oracle XML Developer's Kit 10g supports major XSLT 2.0 features. Oracle XDK also supports DOM 3.0 specification and the XPATH 2.0 specification.
Application Overview
The purpose of this sample is to demonstrate the various new features incorporated in XSLT 2.0. To demonstrate the features, we have an XML file containing the data of all Employees as our base data file. Let us now understand these features:
- Grouping: Grouping data is a very common operation in XSLT. This was very difficult to implement using the XSLT 1.0 standard, but various vendors were providing a specific way of doing it. XSLT 2.0 has mandated the structure of grouping which should be common across all vendors. Grouping can be done in any of the following ways:
- On a specific Key; using the group-by attribute
- At the start of the Group; using the group-starting-with attribute
- At the end of the Group; using the group-ending-with attribute
- By Adjacent Key values; using the group-adjacent attribute
We can also sort the output by the grouping functions as desired using the xsl:sort tag. This sample demonstrates grouping in various forms using the following stylesheets:
GroupDept.xsl: This stylesheet demonstrates a single level grouping using the group-by attribute. The employees are grouped according to their job roles.
-
GroupDeptCountry.xsl: This stylesheet demonstrates the two level grouping (also known as nested grouping). This script uses the group-by attribute. Here, in addition to grouping employees by their job roles, they are further grouped according to their country.
GroupAdjacent.xsl: This stylesheet again demonstrates a form of two level grouping using the group-adjacent attribute. Compare the output generated here with the output of GroupDept.xsl and notice the difference. It will make you appreciate the group-adjacent attribute.
- Create Multiple Result Documents from a single stylesheet: In many real-world applications, you need to create multiple outputs from one XML document. For example, when generating Java docs, you may want to create multiple HTML pages to use frames (demonstrated in
MultipleHTMLOutput.xsl). You may also need to create supplementary files that are referenced by the main output document. For example, when creating an HTML report, you might need to create several Scalable Vector Graphics (SVG) graphics, Cascading Style Sheets (CSS) stylesheets, or metadata files for the report. Since XSLT 1.0 does not have the capability to define multiple outputs in one XSL stylesheet, you have to apply separate XSLT transformations for each one. Although you can write one XSL stylesheet that relies on XSLT parameters to indicate which page to create, the XSL stylesheet needs to execute multiple times and can be quite complicated because of the need to manage these parameters. To simplify these kinds of operations, XSLT 1.0 processors provide various extensions to solve the problem. For example, Oracle XDK provides an <ora:output> extension function. However, these extensions in XSL stylesheets prevent them from being portable across processors. In XSLT 2.0, a new <xsl:result-document> element can be used to define multiple outputs. The sample demonstrates this very useful feature in the stylesheet MultipleOutputByJobs.xsl. Here, the stylesheet creates multiple XML files and each XML file contains the details of all the employees belonging to a particular job role.
- Temporary Tree: This is another feature introduced in XSLT 2.0. Instead of representing the intermediate XSL transformation results and XSL variables as strings, as in XSLT 1.0, the intermediate results and XSL variables, constructed by <xsl:variable>, <xsl:param>, or <xsl:with-param> elements, are stored as a set of document nodes, called Temporary Trees. With temporary trees, you can evaluate the content of a variable or a parameter using the XPath expressions, and modularize the XSL processing. This approach offers a lot of flexibility when applying templates or extracting data from XSL variable or template parameters. The data in XML is no longer treated as plain text but as information. The sample demonstrates this feature using the following scripts:
TemporaryTree.xsl: In this stylesheet, a search is made on the XML for all the Employees who are working in the United States. This information is then stored in a temporary variable. The stylesheet now browses this tree and further searches for employees based on a salary criteria.
-
Combination.xsl: This stylesheet demonstrates how we can use the features of Grouping and Temporary Trees together to our benefit. There are many situations where using both these features together solves the problem in a very simple, effective and vendor independent manner. This stylesheet first searches for all employees working in United States, then it divides them into two senior and junior categories depending on their salaries. After this, they are further grouped according to their respective job roles.
- Character Mapping: Traditionally, we can not define characters like <, >, &, etc. in an XSL document. These were considered invalid or reserved XML characters. We had to use < and other similar notations to use this. But, with XSLT 2.0, this is no longer required. Now, we can first define a character-map and then use the unicode assignment in the stylesheet. This feature is demonstrated in the stylesheet
MultipleHTMLOutput.xsl. This stylesheet also demonstrates how we can generate multiple HTML files using the <xsl:result-document> element. The generated HTML files use frames. To see the errors generated when not using the feature of Character mapping, please run the script MultipleHTMLOutputError.xsl.
- User Defined Functions: XSLT 1.0 lacked the very important feature of defining a function. User-Defined functions form a very integral part in virtually every language. This is solved with the advent of XSLT 2.0. Now, we can describe a function and call it from our stylesheet on passing the required parameters dynamically. This feature is demonstrated in
Functions.xsl where the employees are divided into different groups or bands depending on their salary levels.
Software Requirements
Following is the list of software's required for configuring and running this sample.
Terminology
| Term |
Definition |
| <JAVA_HOME> |
Directory where JAVA is installed. For example, C:\Java |
| <SAMPLE_HOME> |
The directory where the sample is extracted. For example, C:\XSLTSample |
| <XDK_HOME> |
Directory where Oracle XDK is installed. For example, C:\xdk10g |
Configuring and Running the Application
This section describes the steps required to configure and run the scripts within this application using Oracle XDK 10g.
- Unzip the provided
XSLTSample.zip. This creates XSLTSample folder with all the source files. We will refer to this folder as <SAMPLE_HOME>.
- Go to the directory
<SAMPLE_HOME>\src on the command prompt. Make sure that <XDK_HOME>\lib\xmlparserv2.jar is present in the classpath.
- Now, on the command prompt, use the following general command to run the various scripts
java oracle.xml.parser.v2.oraxsl Employee.xml <XSL_FileName>
where, <XSL_FileName> is the name of the XSL script you would like to run. For example,
java oracle.xml.parser.v2.oraxsl Employee.xml GroupDept.xsl
NOTE:
- The script
MultipleOutputByJobs.xsl creates the XML files in the folder <SAMPLE_HOME>\src\xmloutput.
- The script
MultipleHTMLOutput.xsl creates the HTML files in the folder <SAMPLE_HOME>\src\htmloutput. Please go to this folder and open the file index.html in your favorite browser to view the output of the XSL transformation.
Sample Application Files
This section provides a tabular listing of the sample files, along with their respective directory locations and a description of what they do in the overall scheme of the application.
| Directory |
File |
Description |
XSLTSample\doc |
readme.html |
This file |
XSLTSample\src |
Employee.xml |
XML File containing sample data used by all the scripts |
XSLTSample\src |
*.xsl |
Each XSL script demonstrates an XSLT 2.0 feature. To know more about the feature and the file, please go through the Application Overview section or read through the headers of the XSL files. |
Additional References
Please enter your comments about this sample application here. |