[Previous] [Contents] [Next]


Avoiding Flow Objects


In an earlier example, you saw how you to use an XSL style sheet to convert XML code into HTML code. A possibly improper logical extension of this feature (improper because it runs rather counter to the spirit of XSL) is the use of XSL style sheets to allow HTML markup to be mixed with "true" XML markup. This mix creates a kind of hybrid XML/HTML similar to that described in earlier chapters.

Listing 20.20 shows a simple XML document that uses known HTML tags to mark up a table. After all, tables are handled quite well in HTML and offer an easy way to apply table formatting in XSL.

Listing 20.20 Embedding HTML Markup in an XML File


1:  <?xml version="1.0"?>
2:  <?xml:stylesheet type="text/xsl" href="page.xsl"?>
3:  <PAGE>
4:    <PARA>
5:      These are the "new" CDs I found in CD-Warehouse.
6:    </PARA>
7:    <PARA>
8:      <TABLE>
9:        <TITLE>List of New CDs</TITLE>
10:       <TR valign="top">
11:         <TH>Artist</TH><TH>Album Title</TH>
12:       </TR>
13:       <TR valign="top">
14:         <TD>Anouk</TD>
15:         <TD>Together Alone</TD>
16:       </TR>
17:       <TR valign="top">
18:         <TD>Tori Amos</TD>
19:         <TD>Boys for Pele</TD>
20:       </TR>
21:       <TR valign="top">
22:         <TD>Crash Test Dummies</TD>
23:         <TD>The Ghosts that Haunt Me</TD>
24:       </TR>
25:       <TR valign="top">
26:         <TD>Crash Test Dummies</TD>
27:         <TD>A Worm's Life</TD>
28:       </TR>
29:     </TABLE>
30:   </PARA>
31: </PAGE>

If I now develop an XSL style sheet to render this XML inside Internet Explorer 5 (beta preview 2), I can use xsl:copy to pass this XML table code through to the browser and let it apply its default HTML rendering, saving me a lot of effort. Listing 20.21 shows the XSL style sheet and Figure 20.8 shows the resulting display in Internet Explorer 5.

Listing 20.21 Using an XSL Style Sheet to Pass HTML Through Untouched


1:  <?xml version="1.0"?>
2:  <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
3:
4:  <xsl:template><xsl:value-of/></xsl:template>
5:
6:  <xsl:template match="/">
7:    <HTML>
8:      <BODY BGCOLOR="#FFFFFF" LINK="#000066"
9:              VLINK="#666666" TEXT="#000000">
10:       <xsl:for-each select="PAGE">
11:         <xsl:apply-templates/>
12:       </xsl:for-each>
13:     </BODY>
14:   </HTML>
15: </xsl:template>
16:
17: <xsl:template match="PAGE/PARA">
18:   <P><xsl:apply-templates/></P>
19: </xsl:template>
20:
21: <xsl:template match="TABLE">
22:   <TABLE border="1">
23:     <xsl:apply-templates>
24:       <xsl:template>
25:         <xsl:copy>
26:           <xsl:for-each select="@*">
27:             <xsl:attribute>
28:               <xsl:value-of/>
29:             </xsl:attribute>
30:           </xsl:for-each>
31:           <xsl:apply-templates/>
32:         </xsl:copy>
33:       </xsl:template>
34:     </xsl:apply-templates>
35:   </TABLE>
36: </xsl:template>
37:
38: </xsl:stylesheet>


Figure 20.8 The XML/HTML table code rendered as HTML in IE5
There are a couple of features in Listing 20.22 that deserve a special mention. The major part of the work is done by the xsl:copy element (lines 24 to 31). Note, though, that I use an xsl:for-each element (lines 26 to 30) to step through the attributes of each element inside the TABLE element. Using select="@*", I select each of the attributes (of any type) and use the xsl:attribute element (lines 27 to 29) to copy each attribute and its value from the XML element in the input document across to the rendered display in Internet Explorer.

[Previous] [Contents] [Next]