Basic Tutorials
XSL index
XSL can be used to index an XML document.
Where to put the index information
Now look again at the XML document that you have seen in many previous chapters:
<?xml version= "1.0"?>
<CATALOG>
<CD>
<title>empire burlesque</title>
<artist>bob dylan</artist>
<COUNTRY> --> USA </COUNTRY>
<COMPANY> Columbia </COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
.
.
.
To export this XML file as a normal HTML file and index it at the same time, simply add a order-by attribute to the XSL file, as follows:
<xsl:for-each select= "CATALOG/CD" order-by= "+ ARTIST" >
The Order-by property uses a plus sign (+) or minus sign (-) to define the sorted element using ascending or descending, and then an element name.
Now look at the slightly resized XSL stylesheet (or open it in IE5):
<?xml version= ' 1.0 '?>
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >
<xsl:template match= "/" >
<body>
<table border= "2" bgcolor= "Yellow" >
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select= "CATALOG/CD"
order-by= "+ ARTIST" >
<tr>
<td><xsl:value-of select= "TITLE"/></td>
<td><xsl:value-of select= "ARTIST"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</xsl:template>
</xsl:stylesheet>
Converting in Browser
The following are the simple code needed to convert an XML file into HTML in a browser:
<body>
<script language= "JavaScript" >
Load XML
var xml = new ActiveXObject ("Microsoft.XMLDOM")
Xml.async = False
Xml.load ("Cd_catalog.xml")
Load the XSL
var xsl = new ActiveXObject ("Microsoft.XMLDOM")
Xsl.async = False
Xsl.load ("cd_catalog_sort.xsl")
Transform
document.write (Xml.transformnode (XSL))
</script>
</body>
If you are using Internet Explorer 5.0 or later, please click here to view the results.
XSL filter query
XSL can be used to filter an XML file.
Where to place filter information
Now look again at the XML document you've seen several times before:
<?xml version= "1.0"?>
<CATALOG>
<CD>
<title>empire burlesque</title>
<artist>bob dylan</artist>
<COUNTRY> USA </COUNTRY>
<COMPANY> Columbia </COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
.
.
.
To filter an XML file, simply add a filter to the selection attribute of the For-each element in the XSL file, as follows:
<xsl:for-each select= "catalog/cd[artist= ' Bob Dylan '" >
The valid filter operators are:
= equals
!= is not equal to
< less than
> Greater than
Now take a look at the slightly resized XSL style sheet:
<?xml version= ' 1.0 '?>
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >
<xsl:template match= "/" >
<body>
<table border= "2" bgcolor= "Yellow" >
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select= "catalog/cd[artist= ' Bob Dylan '" >
<tr>
<td><xsl:value-of select= "TITLE"/></td>
<td><xsl:value-of select= "ARTIST"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</xsl:template>
</xsl:stylesheet>
Converting in Browser
The following are the simple code needed to convert an XML file into HTML in a browser:
<body>
<script language= "JavaScript" >
Load XML
var xml = new ActiveXObject ("Microsoft.XMLDOM")
Xml.async = False
Xml.load ("Cd_catalog.xml")
Load the XSL
var xsl = new ActiveXObject ("Microsoft.XMLDOM")
Xsl.async = False
Xsl.load ("cd_catalog_filter.xsl")
Transform
document.write (Xml.transformnode (XSL))
</script>
</body>
If you are using Internet Explorer 5.0 or later, please click here to view the results.