Intranet Journal   Earthweb  
Images Events Jobs Premium Services Media Kit Network Map E-mail Offers Vendor Solutions Webcasts

   Intranet Journal Subjects
Search Earthweb

Privacy Policy



internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

internet commerce
Be a Commerce Partner
















 

[ Home | Discussion Forum | How Do I... | Lotus Notes Intranets | Microsoft SharePoint | Products | Shopping  ]

free news!

XML Basics, Part IV: Formatting Output


P.G. Daly
3/15/2004

Go to page: 1 2 

Printer Friendly Version

Welcome to the fourth and final part in this series of XML Basics. We will complete our exploration by using XSLT to format the output of your XML files.

To recap our journey thus far, Part I explored the definition and benefits of XML, Part II examined the detailed concepts of writing and working with XML, and Part III tied it all together with an easy to follow example.

What is XSL?

XSL is an XML-based language used for stylesheets that can be used to transform XML documents into other document types and formats. It consists of the transformation language, abbreviated XSLT, and the formatting language called XSL Formatting Objects. This combination enables two of the key benefits of XML: transforming one document into multiple channels of output and system interoperability.

In order for XSLT to do its magic, you provide it with an input document (source tree) so it can do its thing (transformation) and make it look a particular way in a specified output document (result tree).

XSLT is built on a structure known as an XSL template. This structure is represented by the <xsl:template> element. There are two important pieces to every template element: the match attribute and the actual contents of the template.

The match attribute specifies the pattern in the source tree that will be transformed by a particular template element. In the contents of the template element, you specify what should go in the results tree for each occurrence of the specified pattern.

There are a number of XSLT elements that you could insert into a template to perform a variety of actions. One that is most common is the <xsl:value-of> element. With this element, you can take information from the source tree and add it to the result tree.

For example, let's use an abbreviated piece of our example from Part III:

<movie>
   <title>The Matrix</title>
</movie>

If we wanted to use XSLT to select each occurrence of the <title> element and output its content in the results tree, the XSLT would look as follows:

<xsl:template match="/movie/title"><xsl:value-of select="."/></xsl:template>

The output in this example would be: "The Matrix".

Common XSLT Elements

Templates are the heart and soul of XSLT. A stylesheet is really just a collection of one or more templates that get applied to the input document (the source tree XML document) to create the output document (the result tree that can be a variety of document types).

If an XSL document has more than one template, the order of operations is such that the processor starts by matching the document root to the stylesheet template that best matches it. If there is more than one match, the following order applies: If matching the root element, the first match applies; otherwise, the second (last) match is used.

An important point to remember about the match attribute is that the item matched becomes the context node for that template. What that means is that the matched element becomes the root for that particular template section. Essentially, the match simply creates a subsection of the overall document hierarchy and works from that perspective for the specific template section.

As with many things in the XML world, there are quite a number of XSLT elements that can be used within a template. I will describe the most common ones here and use some of them in the example to follow.

<xsl:stylesheet>
This element is used as the root element of nearly all XSLT stylesheets. Its format is <xsl:stylesheet version="version number" xmlns="path to W3c namespace">. Version number is the current number of the XSLT specification from the W3C. The xmlns is the path to the XML Namespace defined by the W3C for the XSLT Transformation language. Currently, that path is http://www.w3.org/1999/XSL/Transform.

<xsl:template>
As we have already discussed, this element is used to define the templates which make up our XSLT stylesheet. The syntax is <xsl:template match="pattern expression to match" name="template name" priority="number" mode="mode name">

The match attribute is used to indicate the pattern from the source tree in the input document that we wish to select. The name, priority, and mode attributes are optional. The name attribute is used to explicitly name the template, the priority attribute is used to force calling one template over another when more than one can satisfy the match, and mode is used when more than one template satisfies the match but performs different transformations on the content.

<xsl:apply-templates>
This element is used within an XSL template to call other templates.

<xsl:value-of>
As mentioned earlier, this element is used to insert the content specified within the matched element from the source tree into the result tree.

<xsl:output>
This element allows us to specify the output method we will be using, for example, xml, text, or html. It affords us greater control over the actual output.

<xsl:element>
If we don't know ahead of time what elements we need to create in the result tree, <xsl:element> allows us to dynamically create elements during the transformation process.

<xsl:text>
This element simply allows us to insert some specified text (PCDATA) into the output.

Two elements are available in XSLT for conditional processing. These elements allow you to make choices in your stylesheets: <xsl:if> and <xsl:choose>.

<xsl:if>
This is the simpler of the two elements and allows you to evaluate an expression. If it is true, the contents of the <xsl:if> element are evaluated. The syntax looks as follows: <xsl:if test="boolean expression">Some content or XSL elements here</xsl:if>.

<xsl:choose>
This element provides more flexibility than the <xsl:if>. With it, we can make one of any number of choices and even have a default choice. As soon as one of the test expressions evaluate to true, control leaves the <xsl:choose> block. An example looks as follows:

<xsl:choose>
<xsl:when test="element name[test criteria 1]"> Result number one.</xsl:when>
<xsl:when test=="element name[test criteria 2]"> Result number two.</xsl:when>
<xsl:otherwise>The default result.</xsl:otherwise>
</xsl:choose>

<xsl:for-each>
When we need to do particular processing for a number of nodes within the source tree, we need to loop. This element allows us to form a template within a template. Syntax looks as follows: <xsl:for-each select="expression">. Everytime the expression is satisfied, the template associated with the "for-each" is instantiated. See the MovieCatalog example below for this element in action.

<xsl:copy-of>
If you wish to take sections of the source tree and copy them directly to the result tree, you can use this element to do so easily. Simply specify an expression to point to the node(s) required and it will insert the node(s) directly into the result tree along with any attributes and child elements.

Page 2: XSLT Applied to the Movie Catalog Example

Go to page: 1 2

Printer Friendly Version

Of Interest
Intranet eXchange Discussion Board
XML Basics and Benefits
XML Basics, Part II: The Key Concepts
XML Basics Part III: An Example of Well-Formed and Valid XML
XML Basics, Part IV: Formatting Output

email this page

Tutorials
and more at:
Intranet Journal's Tutorials
Intranet Journal Favorites

Creating a PHP-Based Content Management System

The Spyware Guide

Introduction to Microsoft SharePoint Portal

Intranet Journal
Part of the EarthWeb Network

Managing Editor
Intranet Journal

Tom Dunlap

EarthWeb Home Page
Jupitermedia Home Page

Media Kit



internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers