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

本文展示了如何利用XSLT和XML来实现网页中表格的背景色按行交替显示,通过示例解释了如何处理XML文档并应用XSL样式表,创建出美观的表格布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在网络上经常可以看到背景色按行交替设置的表格,比较美观。但未必需要什么高深的服务器技术,用简单的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>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值