XSL Basics Tutorial Chapter Fourth

Source: Internet
Author: User
Tags add filter version xmlns xsl xsl file xsl stylesheet
Basic Tutorials

XSL index
XSL can be used to index an XML document.

Where to put the index information
Now look again at the XML document that you have seen in many previous chapters:

<?xml version= "1.0"?>

<CATALOG>

<CD>

<title>empire burlesque</title>

<artist>bob dylan</artist>

<COUNTRY> --> USA </COUNTRY>

<COMPANY> Columbia </COMPANY>

<PRICE>10.90</PRICE>

<YEAR>1985</YEAR>

</CD>

.

.

.

To export this XML file as a normal HTML file and index it at the same time, simply add a order-by attribute to the XSL file, as follows:

<xsl:for-each select= "CATALOG/CD" order-by= "+ ARTIST" >

The Order-by property uses a plus sign (+) or minus sign (-) to define the sorted element using ascending or descending, and then an element name.

Now look at the slightly resized XSL stylesheet (or open it in IE5):

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

Converting in Browser
The following are the simple code needed to convert an XML file into HTML in a browser:


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


If you are using Internet Explorer 5.0 or later, please click here to view the results.
XSL filter query
XSL can be used to filter an XML file.

Where to place filter information
Now look again at the XML document you've seen several times before:

<?xml version= "1.0"?>

<CATALOG>

<CD>

<title>empire burlesque</title>

<artist>bob dylan</artist>

<COUNTRY> USA </COUNTRY>

<COMPANY> Columbia </COMPANY>

<PRICE>10.90</PRICE>

<YEAR>1985</YEAR>

</CD>

.

.

.

To filter an XML file, simply add a filter to the selection attribute of the For-each element in the XSL file, as follows:

<xsl:for-each select= "catalog/cd[artist= ' Bob Dylan '" >

The valid filter operators are:

= equals

!= is not equal to

< less than

> Greater than

Now take a look at the slightly resized XSL style sheet:

<?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[artist= ' Bob Dylan '" >

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

Converting in Browser
The following are the simple code needed to convert an XML file into HTML in a browser:


<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_filter.xsl")

Transform

document.write (Xml.transformnode (XSL))

</script>

</body>


If you are using Internet Explorer 5.0 or later, please click here to view the results.



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.