Home arrow XML arrow An Introduction to XSLT

An Introduction to XSLT

XSLT allows you to create formatting structures which interpret and modify the existing XML elements. Learn about the syntax of XSLT elements, how the namespace attribute differs depending on the browser in use, and how to transform original XML elements.

Author Info:
By: Dan Wellman
Rating: 4 stars4 stars4 stars4 stars4 stars / 14
September 20, 2004
  1. · An Introduction to XSLT
  2. · Create Elements to Apply the Transformations
  3. · Another Template
  4. · Discussion of the Output
  5. · Page Masters
  6. · Block Level Elements

print this article

An Introduction to XSLT
(Page 1 of 6 )

Support files are available here for this article.

XSL or eXtensible Stylesheet Language began life in much the same way as XML -- as a submission to the W3C. The W3C quickly released several working drafts before the final working draft was released in 2000. It shared some common design goals with XML, namely:

  • A quick design process
  • Transparency to users and developers
  • Ease of use
  • Avoidance of optional features
  • Suitable for use over the Internet

As with XML, these goals were largely met. In October 2001, XSL was split into three separate working specifications by the W3C: XSLT, which is XSL Transformations, XSL-FO, which is XSL Formatting Objects, and Xpath, which defines parts of documents and linking structures. XSL-FO is now formally known as XSL. We will focus mainly on XSLT in the article, but XSL (XSL Formatting Objects) will be looked at briefly towards the end.
Every well-formed XML document has a tree-like structure consisting of parent and child elements. What XSLT does is allow you to create formatting structures which interpret and modify the existing XML elements and can output the results of these structures to HTML (or other output media). It allows you to transform a source tree into a result tree of XML elements.

Both XSLT and XSL must be well-formed documents, and as such XSLT documents must begin with the XML declaration. Following this comes the XSL:stylesheet element, which is the root element of the document and should contain at least the namespace declaration. 

The transformation rules are then added as child elements of the root element. XSLT stylesheets are constructed using template rules that match one or nodes in the XML source documents logical tree. These template rules then create instances of fragments of the result tree.  The XSLT result tree is composed of the hierarchy of nodes in the XML document to which it is applied. 

The syntax of all XSLT elements is the same:

<xsl:tag-name attribute="value">

These elements are reserved in the namespace URI that is set in the documents root element. Template rules also follow a similar syntax:

<xsl:template attribute="value">

There are many template element attributes, some of which we'll explore during the course of this article. As an example of XSLT techniques, we will be creating a stylesheet document that transforms the XML document created in my previous XML article. 

To start with then, as stated above, XSLT documents should be well-formed XML documents, so you start with the XML declaration:

<?xml version="1.0"?>

You then need to add the root element of the stylesheet document with the namespace declaration:

<xsl:stylesheet xmlns:="http//www.w3.org/TR/WD-xsl">

At this point it should be noted that the namespace attribute for XSLT documents differs depending on the browser in use. The namespace declaration used here is for IE5+ browsers. If IE5+ will not be used the following namespace declaration can be substituted:

<xsl:stylesheet xmlns:=http://www.w3.org/1999/XSL/Transform>

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials