Overview

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

Java APIs for XML Kick Start
By Aoyon Chowdhury, Parag Choudhary

Table of Contents
Chapter 6.  XSLT and XPath


XSL Transformations (XSLT), a W3C recommendation, is a language for transforming or changing XML documents into other XML documents that have very different data structures. This process is called tree transformation, in which the source XML data structure, called the source tree, is converted to the required output XML structure, called the output tree, by following the XSLT rules.

Graphically, the tree transformation appears as shown in Figure 6.1.

Figure 6.1. Tree transformation.

graphics/06fig01.jpg

During the tree transformation process, the contents of the source tree can be reordered and filtered, and user-defined content and structure can be added to the result tree.

A transformation in the XSLT language is expressed as a well-formed XML document that follows the XML Namespaces recommendation. The XSLT document may include both elements that are defined by XSLT and elements not defined by XSLT. XSLT-defined elements are distinguished by belonging to a specific XML namespace defined in the URI http://www.w3.org/1999/XSL/Transform.

The document that contains the transformation is called a stylesheet. A stylesheet contains a set of template rules. A template rule has two parts: a template that specifies what to do when the pattern is matched, and a pattern that is matched against nodes in the source tree. The output produced by the template forms a part of the result tree. This enables a stylesheet to be applicable to all documents that have similar source tree structures. A pattern is essentially a mechanism to identify or determine the existence of elements in a particular hierarchy or sequence. This addressing mechanism is defined by the XPath language. For example, consider the following source XML structure:

<carparts>     <carengine>engine 01</carengine>     <carbody>body 01</carbody> </carparts> 

Suppose this XML document is to be transformed into the following XML structure:

<carparts>     <engine>engine 01</engine>     <carbody>body 01</carbody> </carparts> 

To do this transformation, you would need to determine whether there is a carengine element under the carparts element, and if it exists, change it to an engine element. For a transformation such as this, the XSLT syntax would be as follows:

<xsl:template match="/carparts/carengine"> <engine> <xsl:apply-templates/> </engine> </xsl:template> 

In an XSLT document, the set of rules that should be applied to the XML content that matches a specified pattern (written using XPath) is created using the template element.

In this example, the XPath syntax for determining the pattern of the carengine element under the carparts element is /carparts/carengine. XSLT, which is a pattern-matching system, will select all the carengine elements under the carparts element.

When the element matching is done, you can transform it. Within the template, you have access to all the elements that fall under the matched element. To transform the elements, you can apply a template that has been defined anywhere else in the XSLT document. This is done using the apply-template element. In our example, the template dictates that all occurrences of the carengine element under the carparts element must be transformed to an engine element.

Other than transforming XML documents into other XML documents, XSLT can also be used to transform an XML document into other formats, such as HTML, plain text, and even complex binary formats such as PDF.


printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
Top

[0672324342/ch06lev1sec1]

 
 


JavaT APIs for XML Kick Start
JAX: Java APIs for XML Kick Start
ISBN: 0672324342
EAN: 2147483647
Year: 2002
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net