XSL concise tutorial (5) XSL Index

Source: Internet
Author: User

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>

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.