HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
XSLT BASICS PART 1 - INTRODUCTION

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

In this chapter we will provide you with enough information to start building useful XSLT stylesheets. I will introduce a number of the elements that make up the language, providing examples of their use.
Click here to be kept informed of our new Tutorials.


This free tutorial is a sample from the book Professional XSL.


We will also look at a few of the functions built into the language and see how XSLT manages namespaces, whitespace and some other important issues.

To illustrate the concepts I introduce, we will work mainly with two documents, one that is textual in content, and one that is more data oriented. The former is a Shakespeare play (Hamlet), and the latter is a book catalog that could, for example, have been extracted from a relational database.

By the end of the chapter, you will:

  • have a clearer picture of the processing model of XSLT
  • know the difference between push and pull model stylesheets, and when to use each
  • understand the use of the most important XSLT elements
  • understand the use of a few of the built-in functions
  • understand the basic rules of how XSLT copes when there are conflicts in the stylesheet
  • know more about the built-in template rules and how to over ride them

XSLT Processing

Before delving into the detail of XSLT elements and functions, let's start by looking in detail at how an XSLT processor, such as XT, Saxon or MSXML3, processes a document. We will look at the model from an abstract view - becoming an XSLT processor ourselves and working our way through a document and stylesheet. We'll then look at the two fundamental ways in which this model can be used.

More information on these processors can be found in Appendix E. Later in this chapter I will be mainly using XT to process XSLT stylesheets, but any of these processors can be used. XT is similar in use to Instant Saxon, which was introduced in Chapter 2.

The XSLT Processing Model

Although we often talk of an XSLT processor as something that turns one XML document into another (or into an HTML or text document), this is not strictly true. The specification actually talks in terms of a source tree (or input tree) and a result tree. There is therefore an assumption that, for example, if we are starting from a text document rather than an existing DOM tree, it has been turned into some sort of tree structure before the XSLT processor starts its work, and that the result tree will be used for further processing or serialized in some way to create another text document.

The model, including formatting, therefore looks like this:

This concept is simple enough. But you will have read in Chapter 1 that XSLT is a declarative language and uses templates. How does this work in practice? Let's have a look at a simple XML document and stylesheet, and walk through the processing.

Processing a Document

Here is my XML document - it is the book catalog that you will be familiar with if you have read Professional XML (Wrox Press, ISBN 1-861003-11-0), although I have cut it down to just two books, removed some elements and renamed it shortcatalog.xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Catalog>
<Book>
 <Title>Designing Distributed Applications</Title>
 <Authors>
  <Author>Stephen Mohr</Author>
 </Authors>
 <PubDate>May 1999</PubDate>
 <ISBN>1-861002-27-0</ISBN>
 <Price>$49.99</Price>
</Book>
<Book>
 <Title>Professional ASP 3.0</Title>
 <Authors>
  <Author>Alex Homer</Author>
  <Author>Brian Francis</Author>
  <Author>David Sussman</Author>
 </Authors>
 <PubDate>October 1999</PubDate>
 <ISBN>1-861002-61-0</ISBN>
 <Price>$59.99</Price>
</Book>
</Catalog>

We'll look at the XSLT stylesheet we use to transform this document shortly, but let's now become an XSLT processor and see what happens. We already know that, as an XSLT processor, we cannot use the source XML, but need a tree representation based on the structure and content of the document. So here it is:

If you want to view a full version of the above image (65K) click here.

Each node is described by a block of three rectangles. In the top rectangle is the node type, with the node name in the rectangle below it. The bottom rectangle contains an asterisk if the node has element content, and the text if it has text content.

At the top of the tree is the root node or document root. Don't confuse this with the root element (or document element) familiar from XML. The document root is the base of the document, and has the document element (<Catalog>) as a child. It also has the XML declaration and any other top-level nodes (which might be comments or processing instructions) as children. The document element contains two child <Book> elements, and these hold the information about the books.

Continued...


NEXT PAGE



4 RELATED COURSES AVAILABLE
HTML 4.0 INTRODUCTION
To create, format and publish a small website using HTML 4.0. You will learn to create web pages incorporating fo....
A+ MODULE 5 - THE INTERNET
At the end of this course you will be able to: describe the functions of an operating system, describe the featur....
MICROSOFT INTERNET EXPLORER 6.0 INTERNET INTRODUCTION
This course provides readers with an introduction to the concept of the Internet and the opportunity to gain a br....
I-NET+ MODULE 7 - THE WORLD WIDE WEB SERVICE
On completion of this module, readers will be able to: understand how the World Wide Web service works, understan....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Thursday 4th December 2008  © COPYRIGHT 2008 - VISUALSOFT