Use XML+XSLT to implement table background color alternately set by row

Source: Internet
Author: User
Tags implement net version xsl xslt
XML on the network can often see the background color by row alternately set table, more beautiful. But not necessarily what advanced Server technology, with simple xsl+xml still can be very good to achieve them.

For example, we have an XML document that records the URL:

<?xml version= "1.0" encoding= "Utf-8"?>

<?xml-stylesheet type= "text/xsl" href= "xsltfilellink.xsl"?>

<items>

<roomitem>

<text> Sina </text>

<link>http://www.sina.com.cn</link>

</roomitem>

<roomitem>

<text>yahoo</text>

<link>http://www.yahoo.com</link>

</roomitem>

<roomitem>

<text>google</text>

<link>http://www.google.com</link>

</roomitem>

<studyitem>

<text>html Easy Tutorials </text>

<link><a href= "gohttp://www.shanxiwindow.net/teaching/htmlbook/" >go</a></link>

</studyitem>

<studyitem>

<text>javascript Chinese profile </text>

<link>http://www.lib.tsinghua.edu.cn/chinese/INTERNET/JavaScript/</link>

</studyitem>

<studyitem>

<TEXT>MSDN Chinese site </text>

<link>http://www.microsoft.com/china/msdn/default.mspx</link>

</studyitem>

<studyitem>

<text>microsoft. Net Framework SDK QuickStart Tutorials </text>

<link>http://chs.gotdotnet.com/quickstart/default.aspx</link>

</studyitem>

</items>

Now, I would like to classify him as a table with two colors alternating in rows. As shown in the following illustration:

XSL can write this,

<?xml version= "1.0" encoding= "Utf-8"?>

<xsl:stylesheet version= "1.0"

Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" >


<xsl:template match= "/" >

<body>

<!--

This is a XSLT template file. Fill in the, with the

XSL elements which would transform your XML to XHTML.

-->

<H1>XSLT Application Test

<table width= "100%" border= "1" >

<tr bgcolor= "#C9BBAD" >

<th>name</th>

<th>link</th>

</tr>

<xsl:for-each select= "Items/roomitem" >

<xsl:choose>

<xsl:when test= "(Position () mod 2) = 0" >

<tr bgcolor= "#C9BBAD" >

<td>

<xsl:value-of select= "Text"/>

</td>

<td>

<xsl:value-of select= "link"/>

</td>

</tr>

</xsl:when>

<xsl:otherwise>

<tr>

<td>

<xsl:value-of select= "Text"/>

</td>

<td>

<xsl:value-of select= "link"/>

</td>

</tr>

</xsl:otherwise>

</xsl:choose>

</xsl:for-each>

</table>

<br/>

<table width= "100%" border= "1" >

<tr bgcolor= "#C9BBAD" >

<th>name</th>

<th>link</th>

</tr>

<xsl:for-each select= "Items/studyitem" >

<xsl:choose>

<xsl:when test= "(Position () mod 2) = 0" >

<tr bgcolor= "#C9BBAD" >

<td>

<xsl:value-of select= "Text"/>

</td>

<td>

<xsl:value-of select= "link"/>

</td>

</tr>

</xsl:when>

<xsl:otherwise>

<tr>

<td>

<xsl:value-of select= "Text"/>

</td>

<td>

<xsl:value-of select= "link"/>

</td>

</tr>

</xsl:otherwise>

</xsl:choose>

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