用xml+xslt实现表格背景色按行交替设置

王朝other·作者佚名  2006-02-01
窄屏简体版  字體: |||超大  

在网络上经常可以看到背景色按行交替设置的表格,比较美观。但未必需要什么高深的服务器技术,用简单的xsl+xml照样能很好的实现他们。

比如,我们有这么一个记录网址的xml文档:

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

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

<items>

<roomitem>

<text>新浪</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简易教程</text>

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

</studyitem>

<studyitem>

<text>javascript中文简介</text>

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

</studyitem>

<studyitem>

<text>msdn中文站点</text>

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

</studyitem>

<studyitem>

<text>Microsoft .Net 框架 SDK 快速入门教程</text>

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

</studyitem>

</items>

现在,我希望按照其分类,把他表现成两个颜色按行交替的表格。如下图所示:

Xsl可以如此写出,

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

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<!--

This is an XSLT template file. Fill in this area with the

XSL elements which will transform your XML to XHTML.

-->

<h1>XSLT应用测试</h1>

<hr />

<h3>功能类:</h3>

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

<h3>资源类:</h3>

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

</html>

</xsl:template>

</xsl:stylesheet>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航