Tutorials | index
Five. The index of the XSL
How do I establish an XSL index if I need to arrange the display of the elements in a certain order?
Let's take a look at the previous example, or the code:
<?xml version= "1.0" encoding= "Iso8859-1"?>
<CATALOG>
<CD>
<title>empire burlesque</title>
<artist>bob dylan</artist>
<COUNTRY> USA </COUNTRY>
<COMPANY> Columbia </COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
.
.
.
When an XML document is converted into an HTML file, the index should be established at the same time. The easy way to do this is to add a order-by attribute to your for-each element, like this:
<xsl:for-each select= "CATALOG/CD" order-by= "+ ARTIST" >
The Order-by property comes with a "+" or "-" symbol, which is used to define the index in ascending or descending order. The name behind the symbol is the keyword to index.
For example (cd_catalog_sort.xsl):
<?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>
Finally, we use the HTML code below to display the index results, and you can try them yourself.
<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>