An XSL Concise tutorial (7) XSL control statement _xml/rss

Source: Internet
Author: User
Tags xsl
Original: A Egil refsnes translation: Atzie

Seven. XSL Control statements


1. Conditional Statement If...then

XSL also has conditional statements (hehe ~ ~ Good, like the program language). The specific syntax is to add a xsl:if element, similar to this

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

... some output ...

</xsl:if>


The example above is rewritten as:

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


2. Choose of XSL

The purpose of choose is to have multiple conditions, giving different display results. 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 example above is rewritten as:

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

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

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.