XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

Getting started with XSLT and XPath

August 23, 2000

Examining working stylesheets can help us understand how we use XSLT and XPath to perform transformations. This article first dissects some example stylesheets before introducing basic terminology and design principles.

2.1  Stylesheet examples

Let's first look at some example stylesheets using two implementations of XSLT 1.0 and XPath 1.0: the XT processor from James Clark, and the third web release of Internet Explorer 5's MSXML Technology Preview.

These two processors were chosen merely as examples of, respectively, standalone and browser-based XSLT/XPath implementations, without prejudice to other conforming implementations. The code samples only use syntax conforming to XSLT 1.0 and XPath 1.0 recommendations and will work with any conformant XSLT processor.

Note: The current (4/14/2000) Internet Explorer 5 production release supports only an archaic experimental dialect of XSLT based on an early working draft of the recommendation. The examples in this book will not run on the production release of IE5. The production implementation of the old dialect is described in http://msdn.microsoft.com/xml/XSLGuide/conformance.asp.

2.1.1  Some simple examples

Consider the following XML file hello.xml obtained from the XML 1.0 Recommendation and modified to declare an associated stylesheet:

01  <?xml version="1.0"?>
02  <?xml-stylesheet type="text/xsl" href="hello.xsl"?>
03  <greeting>Hello world.</greeting>
Example 2-1: The first sample instance in XML 1.0 (modified)

We will use this simple file as the source of information for our transformation. Note that the stylesheet association processing instruction in line 2 refers to a stylesheet with the name "hello.xsl" of type XSL. Recall that an XSLT processor is not obliged to respect the stylesheet association preference, so let us first use a standalone XSLT processor with the following stylesheet hellohtm.xsl:

01  <?xml version="1.0"?><!--hellohtm.xsl-->
02  <!--XSLT 1.0 - http://www.CraneSoftwrights.com/training -->
03  <html xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
04        xsl:version="1.0">
05   <head><title>Greeting</title></head>
06   <body><p>Words of greeting:<br/>
07     <b><i><u><xsl:value-of select="greeting"/></u></i></b>
08     </p></body>
09  </html>
Example 2-2: An implicitly-declared simple stylesheet

This file looks like a simple XHTML file: an XML file using the HTML vocabulary. Indeed, it is just that, but we are allowed to inject into the instance XSLT instructions using the prefix for the XSLT vocabulary declared on line 3. We can use any XML file as an XSLT stylesheet provided it declares the XSLT vocabulary within and indicates the version of XSLT being used. Any prefix can be used for XSLT instructions, though convention often sees xsl: as the prefix value.

Line 7 contains the only XSLT instruction in the instance. The xsl:value-of instruction uses an XPath expression in the select= attribute to calculate a string value from our source information. XPath views the source hierarchy using parent/child relationships. The XSLT processor's initial focus is the root of the document, which is considered the parent of the document element. Our XPath expression value "greeting" selects the child named "greeting" from the current focus, thus returning the value of the document element named "greeting" from the instance.

Using an MS-DOS command-line invocation to execute the standalone processor, we see the following result:

01  X:\samp>xt hello.xml hellohtm.xsl hellohtm.htm
02  X:\samp>type hellohtm.htm
03  <html>
04  <head>
05  <title>Greeting</title>
06  </head>
07  <body>
08  <p>Words of greeting:<br>
09  <b><i><u>Hello world.</u></i></b>
10  </p>
11  </body>
12  </html>
13  
14  X:\samp>
Example 2-3: Explicit invocation of Example 2-2

Note how the end result contains a mixture of the stylesheet markup and the source instance content, without any use of the XSLT vocabulary. The processor has recognized the use of HTML by the name of the document element and has engaged SGML lexical conventions.

The SGML lexical conventions are evidenced on line 8 where the <br> empty element has been serialized without the XML lexical convention for the closing delimiter. This corresponds to line 6 of our stylesheet in Example 2-2 where this element is marked up as <br/> according to XML rules. Our inputs are always XML but the XSLT processor may recognize the output as being HTML and serialize the result following SGML rules.

Consider next the following explicitly-declared XSLT file hello.xsl to produce XML output using the HTML vocabulary, thus the output is serialized as XHTML:

01  <?xml version="1.0"?><!--hello.xsl-->
02  <!--XSLT 1.0 - http://www.CraneSoftwrights.com/training -->
03  
04  <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
05                 version="1.0">
06  
07  <xsl:output method="xml" omit-xml-declaration="yes"/>
08  
09  <xsl:template match="/">
10      <b><i><u><xsl:value-of select="greeting"/></u></i></b>
11  </xsl:template>
12  
13  </xsl:transform>
Example 2-4: An explicitly-declared simple stylesheet

This file explicitly declares the document element of an XSLT stylesheet with the requisite XSLT namespace and version declarations. Line 7 declares the output to follow XML lexical conventions and that the XML declaration is to be omitted from the serialized result. Lines 9 through 11 declare the content of the result that is added when the source information position matches the XPath expression in the match= attribute on line 9. The value of "/" matches the root of the document, hence, this refers to the XSLT processor's initial focus.

The result we specify on line 10 wraps our source information in the HTML elements without the boilerplate used in the previous example. Line 13 ends the formal specification of the stylesheet content.

Using an MS-DOS command-line invocation to execute the XT processor we see the following result:

01  X:\samp>xt hello.xml hello.xsl hello.htm
02  
03  X:\samp>type hello.htm
04  <b><i><u>Hello world.</u></i></b>
05  X:\samp>
Example 2-5: Explicit invocation of Example 2-4

Using a non-XML-aware browser to view the resulting HTML in Example 2-5 we see the following on the canvas (the child window is opened using the View/Source menu item):

Figure 2-1: 
An non-XML-aware browser viewing the source of a document
Figure 2-1: An non-XML-aware browser viewing the source of a document

Using an XML-aware browser recognizing the W3C stylesheet association processing instruction in Example 2-1, the canvas is painted with the HTML resulting from application of the stylesheet (the child window is opened using the View/Source menu item):

Figure 2-2: 
An XML-aware browser viewing the source of a document
Figure 2-2: An XML-aware browser viewing the source of a document

The canvas content matches what the non-XML browser rendered in Figure 2-1. Note that View/Source displays the raw XML source and not the transformed XHTML result of applying the stylesheet.

Note:

I found it very awkward when first using browser-based stylesheets to diagnose problems in my stylesheets. Without access to the intermediate results of transformation, it is often impossible to ascertain the nature of the faulty HTML generation. One of the free resources found on the Crane Softwrights Ltd. web site is a script for standalone command-line invocation of the MSXML XSLT processor. This script is useful for diagnosing problems by revealing the result of transformation. This script has also been used extensively by some to create static HTML snapshots of their XML for delivery to non-XML-aware browsers.

This is a prose version of an excerpt from the book "Practical Transformation Using XSLT and XPath" (Eighth Edition ISBN 1-894049-05-5 at the time of this writing) published by Crane Softwrights Ltd., written by G. Ken Holman; this excerpt was edited by Stan Swaren, and reviewed by Dave Pawson.

Pages: 1, 2, 3

Next Pagearrow