XSL concise tutorial (II)

Source: Internet
Author: User
Tags xsl
XSL concise tutorial (II)

5. XSL Index
Chapter Guide

 

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, or this 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>

 
1. XSL entry

Ii. XSL Conversion

Iii. XSL-implementation on the client

Iv. XSL --- server-side implementation

5. XSL Index

Vi. XSL filtering and query

VII. XSL control statements


 
Vi. XSL filtering and query chapter Guide
What should we do if we want to display only XML data that meets certain conditions? In the preceding example code, we only need to add parameters to the select attribute of the XSL: For-each element. For example:

<XSL: For-each select = "catalog/CD [artist = 'Bob dylan']">

The logical options of parameters include:

= (Equal)

=! (Not equal)

& Lt & less

& Gt & greater than or equal

The same as the previous 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 [artist = 'Bob dylan']">

<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>

You can test the results by yourself.

 

1. XSL entry

Ii. XSL Conversion

Iii. XSL-implementation on the client

Iv. XSL --- server-side implementation

5. XSL Index

Vi. XSL filtering and query

VII. XSL control statements
 
VII. XSL control statement section guide
1. conditional statement if... then

XSL also has conditional statements (haha ~~ It's amazing, like programming languages ). The specific syntax is to add an XSL: If element, similar to this

<XSL: If match = ". [artist = 'Bob dylan']">

... Some output...

</XSL: If>

Note:

1. the format I have seen is: <XSL: If test = ". [artist = 'Bob dylan'] "> instead of <XSL: If match = ". [artist = 'Bob dylan'] ">

2. There is a "." in "", which should not be missed.

The preceding example is rewritten as follows:

<? 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">

<XSL: If match = ". [artist = 'Bob dylan']">

<Tr>

<TD> <XSL: value-of select = "title"/> </TD>

<TD> <XSL: value-of select = "artist"/> </TD>

</Tr>

</XSL: If>

</XSL: For-each>

</Table>

</Body>

</Html>

</XSL: Template>

</XSL: stylesheet>

2. Choose of XSL

The purpose of choose is to display different results when multiple conditions exist. The specific syntax is to add a set of XSL: Choose, XSL: When, XSL: otherwise elements:

<XSL: Choose>

<XSL: When match = ". [artist = 'Bob dylan']">

... Some code...

</XSL: When>

<XSL: otherwise>

... Some code ....

</XSL: otherwise>

</XSL: Choose>

The preceding example is rewritten as follows:

<? 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">

<Tr>

<TD> <XSL: value-of select = "title"/> </TD>

<XSL: Choose>

<XSL: When match = ". [artist = 'Bob dylan']">

<TD bgcolor = "# ff0000"> <XSL: value-of select = "artist"/> </TD>

</XSL: When>

<XSL: otherwise>

<TD> <XSL: value-of select = "artist"/> </TD>

</XSL: otherwise>

</XSL: Choose>

</Tr>

</XSL: For-each>

</Table>

</Body>

</Html>

</XSL: Template>

</XSL: stylesheet>
 

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.