[Previous] [Contents] [Next]


XSL2


XSL2 was first released in draft form in July 1998. The latest and current version was released as a working draft on December 16, 1998 (from now on I shall simply refer to it as XSL because it makes all earlier versions obsolete).

There are quite a few sections of the draft still unfinished and several questions that still need to be answered before the proposal can be properly adopted as a standard. As was also true of the previous release, publication was already a month late by the time the draft came out and I suppose the authors felt that it was better to let everyone see at least a partial version. Fortunately, this hasn't stopped software developers. Within a month of the last release there was an XSL processor available for free download (imaginatively called XSLProcessor, available from
http://www.inria.fr/koala/XML/ xslProcessor). This tool is a Java XSL processor that takes an XSL file and an XML file and creates one or more HTML files.

In between this release and the last, Microsoft released a second beta preview of Internet Explorer 5. Microsoft incorporated some changes that were not made public until the latest release of the XSL draft and so for a short time it appeared as if they had implemented a private version of XSL.

More interestingly, Indelv (http://www.indelv.com) released a so-called Technology Introduction Preview Release package that processes and displays XML code using XSL code. The current release of this software is extremely limited (some of the editing functions have been disabled, although it does have limited VML support, which is quite exciting to see), but with a little hacking it's possible to get the software to process your own code.

The key to hacking (I use the word in its original sense of experimenting with software) is to edit the file navigation.xml. If you add the following lines to the end of this file:

<branch collapsed="no">
<branch-title
href="simon.xml">Simon's Files
</branch-title>
<branch>
<branch-title
href="simon1.xml">File 1
</branch-title>
</branch>
<branch>
<branch-title
href="simon2.xml">File 2
</branch-title>
</branch>
</branch>

Where you replace the filenames simon1.xml and simon2.xml with the name of your files, you can get Indelv to read any XML file you want.

Before we get too involved in the theory, let's look at a short example of an XML file and its associated XSL file. Listing 20.3 shows an XML example, Listing 20.4 shows the XSL code, and Figure 20.1 shows what the result looks like in Indelv.

Listing 20.3 A Sample XML File
1:  <?xml version="1.0"?>
2:  <?xml:stylesheet type="text/xsl" href="simon1.xsl"?>
3:  <content>
4:
5:  <hr/>
6:
7:  <title>THE MORON</title>
8:  <poem>
9:  <line>See the happy moron</line>
10: <line>He doesn't give a damn.</line>
11: <line>I wish I were a moron,</line>
12: <line><stress>My God! perhaps I am.</stress></line>
13: </poem>
14:
15: </content>

Note how the XSL style sheet is linked to the XML document by means of an XML processing instruction (this is the approved manner of linking the two together). You can also link a CSS style sheet to an XML file using the same mechanism (the file would then be a CSS file and the type would be text/css).
The latest release of the XSL working draft is so new that few tools have had a chance to implement the changes in the syntax of XSL. Generally, to make the code examples shown in this chapter comply with the new syntax, you should replace all occurrences of xsl:process-childen with xsl:apply-templates.

Note, though, that IE5 doesn't support flow objects and so you won't be able to display much of this code anyway.



Listing 20.4 The Associated XSL File
1:  <xsl:stylesheet
2:    xmlns:xsl="http://www.w3.org/TR/WD-xsl"
3:    xmlns:fo="http://www.w3.org/TR/WD-xsl/FO"
4:    result-ns="fo">
5:
6:    <xsl:template match="/">
7:      <fo:sequence
8:          font-size="12pt"
9:          indent-end=8pt
10:         indent-start=8pt>
11:     <xsl:process-children/>
12:     </fo:sequence>
13:   </xsl:template>
14:
15:   <xsl:template match="hr">
16:     <fo:rule-graphic
17:         space-before-optimum="12pt"
18:         space-after-optimum="12pt"
19:         graphic-line-thickness='2pt'>
20:       <xsl:process-children/>
21:     </fo:rule-graphic>
22:   </xsl:template>
23:
24:   <xsl:template match="poem">
25:     <fo:block-level-box
26:         indent-start="1in"
27:         indent-end="1in"
28:         background-color='blue'
29:         graphic-line-thickness='3pt'>
30:     <xsl:process-children/>
31:     </fo:block-level-box>
32:  </xsl:template>
33:
34:  <xsl:template match="line">
35:     <fo:block
36:         font-style="italic"
37:         space-before-optimum="2pt"
38:         space-after-optimum="2pt">
39:       <xsl:process-children/>
40:     </fo:block>
41:   </xsl:template>
42:
43:  <xsl:template match="title">
44:     <fo:block
45:        font-size="14pt"
46:        font-weight="bold"
47:        space-before-optimum="12pt"
48:        space-after-optimum="8pt">This poem is called '
49:      <xsl:process-children/>
50:        <xsl:text> '</xsl:text>
51:    </fo:block>
52:  </xsl:template>
53:
54:  <xsl:template match="stress">
55:     <fo:sequence
56:        font-size="14pt"
57:        font-weight="bold"
58:        font-style="normal"
59:        color='white'>
60:      <xsl:process-children/>
61:     </fo:sequence>
62:  </xsl:template>
63:
64: </xsl:stylesheet>

Note how the XSL style sheet is a well-formed XML document. In fact, the style sheet can also be validated because there's a style sheet DTD included as an appendix to the working draft. (This alone is a major step forward because you can now check that the syntax of your style sheets is correct by using the same tools that you use for your XML files-an option that was sadly missing from CSS.)
The great thing about this new style sheet syntax is that it is not only well-formed, it should also be valid. If you point your Web browser to the XSL proposal (http://wwww.w3c.org/TR/WD-xsl) and save the file to your hard disk, you can extract the DTD contained in Appendix A and use it to debug your style sheets. This will save you a lot of effort in trying to check for those annoyingly simple syntax errors that seem to be so easy to make, but so hard to find.

The first part of the style sheet is more or less standard. This prologue is the standard XML document prologue plus some namespace declarations. The XSL namespace should always be used as it is shown here, but the result namespace can really be anything you like.

There are still a lot of issues to be solved concerning namespaces, but for the time being we have to go ahead with what we've got. In theory, if you declare your own namespace there should be an accompanying URL that points to an explanation of the semantics of the elements. This, however, is not checked and so any URL is acceptable until a resolution mechanism is found.

Figure 20.1 A rendered XML file showing properties applied by an XSL file.

[Previous] [Contents] [Next]