Direct Processing
If your source XML document has a known, regular structure (such as you usually see in tables and catalogs), you can use the for-each statement to recursively process down the tree. Listing 20.10 shows a sample XML file taken from a database containing listings of the names of tracks in a CD collection.
Listing 20.10 A Regularly Structured XML File
1: <?xml version="1.0"?> 2: <?xml:stylesheet type="text/xsl" href="cd1.xsl"?> 3: <CDs> 4: <CD> 5: <title>Boys for Pele</title> 6: <artist>Tori Amos</artist> 7: <tracks> 8: <track>Horses</track> 9: <track>Blood Roses</track> 10: <track>Father Lucifer</track> 11: <track>Professional Widow</track> 12: <track>Mr. Zebra</track> 13: <track>Marianne</track> 14: <track>Caught a Lite Sneeze</track> 15: <track>Muhammad My Friend</track> 16: <track>Hey Jupiter</track> 17: <track>Way Down</track> 18: <track>Little Amsterdam</track> 19: <track>Talula</track> 20: <track>Not the Red Baron</track> 21: <track>Agent Orange</track> 22: <track>Doughnut Song</track> 23: <track>In the Springtime of his Voodoo</track> 24: <track>Putting the Damage on</track> 25: <track>Twinkle</track> 26: </tracks> 27: </CD> 28: <CD> 29: <title>The Ghosts that Haunt Me</title> 30: <artist>Crash Test Dummies</artist> 31: <tracks> 32: <track>Winter Song</track> 33: <track>Comin' Back Soon (The Bereft Man's Song)</track> 34: <track>Superman's Song</track> 35: <track>The Country Life</track> 36: <track>Here on Earth (I'll have my Cake)</track> 37: <track>The Ghosts that Haunt Me</track> 38: <track>Thick-Necked Man</track> 39: <track>Androgynous</track> 40: <track>The Voyage</track> 41: <track>At My Funeral</track> 42: </tracks> 43: </CD> 44: </CDs>
The XSL style sheet shown in Listing 20.11 creates an HTML document containing a table with a row for each track element.
Listing 20.11 Using for-each to Recursively Process the XML File
1: <?xml version="1.0"?> 2: <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> 3: 4: <xsl:template match="/"> 5: <xsl:apply-templates/> 6: </xsl:template> 7: 8: <xsl:template match="track"> 9: <xsl:apply-templates/>, </xsl:template> 10: 11: <xsl:template match="track[end()]"> 12: <xsl:apply-templates/>. </xsl:template> 13: 14: <xsl:template match="textnode()"> 15: <xsl:get-value/></xsl:template> 16: 17: <xsl:template match="/"> 18: <HTML> 19: <HEAD> 20: <TITLE>My CD Collection</TITLE> 21: </HEAD> 22: <BODY> 23: <H1>My CD Collection</H1> 24: <TABLE BORDER="3" CELLSPACING="2" CELLPADDING="6"> 25: <col bgcolor="yellow"/> 26: <THEAD align="left" bgcolor="silver"> 27: <TH>Artist</TH><TH>Album Title</TH><TH>Tracks</TH> 28: </THEAD> 29: <TBODY> 30: <xsl:for-each select="CDs/CD"> 31: <TR> 32: <TD><font color="red" size="5"> 33: <B><xsl:value-of select="artist"/></B></font></TD> 34: <TD><B><I><xsl:value-of select="title"/></I></B></TD> 35: <TD><xsl:apply-templates select="tracks/track"/></TD> 36: </TR> 37: </xsl:for-each> 38: </TBODY> 39: </TABLE> 40: </BODY> 41: </HTML> 42: </xsl:template> 43: 44: </xsl:stylesheet>
Figure 20.7 The XML code rendered as HTML in IE5.
|
| Note how surprisingly easy it is to convert XML code into HTML using XSL style sheets! All you have to do is select the element or elements that you are interested in, insert the HTML starting tag, process the element content, then insert the HTML closing tag. This is probably the simplest method that you will see in this whole book and an excellent reason for learning XSL. |
|