Original: Jan Egil refsnes
5. XSL Index
If I want to arrange the display of elements in a certain order, how should I create an XSL index?
Let's take a look at the previous example.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 to an HTML file, the index should be created at the same time. The simple method is to add an order-by attribute to your for-each element, as shown in the following figure:
<XSL: For-each select = "catalog/CD" Order-by = "+ artist">
The Order-by attribute carries a "+" or "-" symbol to define the index method, in ascending or descending order. The name after the symbol is the keyword to be indexed.
For example (cd_catalog_sort.xsl ):
<? XML version = '1. 0'?>
<XSL: stylesheet xmlns: XSL = "http://www.w3.org/TR/WD-xsl">
<XSL: template match = "/">
<HTML>
<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>
</Html>
</XSL: Template>
</XSL: stylesheet>
Finally, we use the following HTML code to display the index results. You can try it on your own.
<HTML>
<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>
</Html>