marginwidth="0" marginheight="0" src="http://218.16.120.35:65001/PC/Global/images/b.html" frameborder="0" width="728" scrolling="no" height="90">
摘要:本文通过实际的例子来说明如何在XSL中实现对XML数据转换成完美的多列表格。
在利用XSL对XML进行转换时,有时候需要把XML转换成多列的Table元素,这个问题经常会困扰许多人,如果不生成Table的话,只需要对循环中的节点进行位置取模后判断,然后用<br/>换行即可。但有时候为了用户需要和界面的美观,需要生成多行多列的Table,常用的方法是采用following-sibling进行判断,比如下面的代码:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:copyRight="http://xml.sz.luohuedu.net/">
<xsl:template match="/">
<table bgcolor="snow" border="1" cellpadding="5" cellspacing="2" borderColor="darkorange"
style="font-size:9pt">
<xsl:for-each select="/Items/Item[position() mod 3 = 1]">
<tr>
<td width="33%" align="center" valign="middle">
<xsl:apply-templates select="."/>
</td>
<td width="34%" align="center" valign="middle">
<xsl:apply-templates select="following-sibling::Item[position() = 1]"/>
</td>
<td width="33%" align="center" valign="middle">
<xsl:apply-templates select="following-sibling::Item[position() = 2]"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="/Items/Item">
<a target="_blank">
<xsl:attribute name="href">
<xsl:if test="contains(Url,'@')">mailto:</xsl:if>
<xsl:value-of select="Url"/>
</xsl:attribute>
<xsl:value-of select="Title"/>
</a>
</xsl:template>
</xsl:stylesheet><