XSL Basics Tutorial Chapter Fifth

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

XSL condition if
XSL can filter information from an XML document with an if statement.

Where do I put the IF condition
Now take a look at the XML document you've seen several times:

<?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 place a condition test if command on the contents of a file, simply add a xsl:if element to the XSL document, as follows:

<xsl:if match= ". [artist= ' Bob Dylan '] >

... Some output ...

</xsl:if>

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

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


</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_if.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 Conditional selection Choose
XSL can filter XML documents using conditional selection.

Where to place selection criteria
Take a look at the XML documents that you've seen almost every previous chapter:

<?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 insert a conditional selection test on the contents of a file, simply add the Xsl:choose, Xsl:when, and xsl:otherwise elements to the XSL document as follows:

<xsl:choose>

<xsl:when match= ". [artist= ' Bob Dylan '] >

... Some code ...

</xsl:when>

<xsl:otherwise>

... Some code ...

</xsl:otherwise>

</xsl:choose>

Now let's 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" >

<tr>

<td><xsl:value-of select= "TITLE"/></td>

<xsl:choose>

<xsl:when match= ". [artist= ' Bob Dylan '] >

&LT;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>


</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_choose.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.