|
8. Making HTML
One little stage is still left, and that
is formatting the XML files so that they make nice HTML files that we can render
in a browser. A full explanation of all things you can do in XSL is out of the
scope of this document, but this very basic style sheet should provide a good
starting point for anyone willing to get his or her hands dirty.
Code listing 8.1: This very basic XSL style sheet creates a list of all albums in the libary |
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xsl:version="1.0">
<xsl:key name="album-sort-keys" match="AlbumItem" use="SortKey" />
<xsl:template match="xmlLibrary">
<html>
<head>
<title>Music Library - Exported from iTunes</title>
</head>
<body>
<xsl:for-each select="AlbumItem">
<xsl:sort select="SortKey"/>
<xsl:value-of select="Album" /> - <xsl:value-of select="Artist" /><br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|
This style sheet basically consists of only a couple of processing instructions
that are specific to XSL. Anyone that has ever seen some HTML should be able
to recognize the HTML structure within this style sheet: the <html> section,
the <head> and the <body> are there.
In the body there is just one big loop, indicated by the xsl:for-each tag. The
first instruction indicates how to get the albums sorted - there is a key ready
for your usage in the XML that is generated by the script, and using the
<xsl:value-of> directive, it selects artist and album, prints a dash in
between them and a newline (<br>) after it.
How easy can it be? You can start from this to lay out the HTML like you want
it and use the other XSL files in the package for further inspiration to do the
trickier things. Good luck!
|