Introduction to XSLT

It introduces XSLT, going through the basics of using it to transform XML on the client-side, by way of easy-to-follow tutorial examples (we have included the first three examples here). Chapter 6 of the book takes client-side XSLT to a more advanced level, and Chapters 8-11 include coverage of server-side XSLT usage.

 

This sample is taken from Chapter 5 "Introduction to XSLT"of the glasshaus title "Practical XML for the Web".

Example 1: Saving Bandwidth by Using XSLT for Headers and Footers

Let's start by taking a normal web page, and seeing how we can use XSLT to add some standard content to the page. In this example, we'll add a header and footer to a simple page of XHTML.

All of the examples in this chapter can be downloaded from the glasshaus web site at http://www.glasshaus.com. The downloadable versions have additional comments so you don't need to keep referring back to the book as you read them.

The benefits of doing this in XSLT are as follows:

·          We only need to store one copy of the header and footer code to cover every page on the site, making it easy to change them globally without editing every single page.

·          Users only need to download the header and footer XSLT code once, even though they are used on every page in the site. This can give bandwidth savings, which is important if your service provider charges by the megabyte, or if you are supporting modem users.

·          Server processing is reduced as all the work is being done on the client.

How else could we do it? We could manually add the header and footer to every web page, but this would result in a site maintenance nightmare, especially for a large site. The other alternatives are as follows:

·          By using Server-Side Includes (SSI) to include standard header and footer content on every page.

·          By using a scripting language (such as ASP, PHP, or JSP) to generate web pages with the header and footer included.

Although these methods share the first advantage of only storing one copy of the header and footer, they do not reduce the bandwidth or server load.

Take it From the Top

Here's our starting XHTML page, dinosaur.html. It's nice and simple, not very pretty, but functional (don't worry: we'll beautify it later):

<html lang="en">

<head>

  <title>A simple HTML page</title>

  <style type="text/css">

    body { font-family: Verdana, Times, Serif; }

  </style>

</head>

<body>

  <h1>My big list of dinosaurs</h1>

  <h2>Brontosaurus</h2>

  A Brontosaurus is big and scary.

  <ul>

    <li><b>Weight:</b> 200 tons</li>

    <li><b>Length:</b> 90 m</li>

    <li><b>Color:</b> Blue</li>

  </ul>

  <h2>Tyrannosaurus Rex</h2>

  A Tyrannosaurus Rex would eat you.

  <ul>

    <li><b>Weight:</b> 50 tons</li>

    <li><b>Length:</b> 20 m</li>

    <li><b>Color:</b> Red</li>

  </ul>

  <h2>Stegosaurus</h2>

  A Stegosaurus has lots of spiny plates.

  <ul>

    <li><b>Weight:</b> 25 tons</li>

    <li><b>Length:</b> 20 m</li>

    <li><b>Color:</b> Green</li>

  </ul>

</body>

</html>


And here's that code in action:

(Well, we did say it wasn't that exciting.)

Let's spice up our page by adding a header and footer. We could of course just add some extra header and footer XHTML into our page and do things the old-fashioned way, but that wouldn't give us the benefits we describe above. Instead, we'll add an extra line to the top of dinosaur.html, and save the resulting code as dinosaur_1.xml:

<?xml-stylesheet type="text/xsl" href="headerfooter_1.xsl" ?>

<html lang="en">

<head>

  ...

This new line performs one very important task: referencing our first XSLT stylesheet file, headerfooter_1.xsl. The rest of our XHTML stays the same.

During this chapter, we will encounter slight differences between how various browsers deal with stylesheets. In a minute, we'll learn a useful trick to specify different stylesheets for different browsers, but for now we'll keep things simple and just specify headerfooter_1.xsl.

You may have spotted that our new page has an .xml extension rather than .html. There's a reason for this. In order to start using XSLT to transform our page, we need to be sure that the page is well-formed XML, rather than just any old HTML we've chucked together. Our new page is in fact XHTML, as introduced in Chapter 2  we therefore give it the .xml extension.

George Petrov

George PetrovGeorge Petrov is a renowned software writer and developer whose extensive skills brought numerous extensions, articles and knowledge to the DMXzone- the online community for professional Adobe Dreamweaver users. The most popular for its over high-quality Dreamweaver extensions and templates.

George is also the founder of Wappler.io - the most Advanced Web & App Builder

See All Postings From George Petrov >>

Comments

Be the first to write a comment

You must me logged in to write a comment.