XSL Concise tutorial (5) Index of XSL

Source: Internet
Author: User
Tags version xsl
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>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.