读书人

xsl 输出一个表格如何做啊

发布时间: 2012-03-07 09:13:51 作者: rapoo

xsl 输出一个表格怎么做啊?
有一个关于城市的数据,部分如下:
<city-list>
<city>
<id> 0 </id>
<name> 北京 </name>
</city>
<city>
<id> 1 </id>
<name> 上海 </name>
</city>
<city>
<id> 2 </id>
<name> 深圳 </name>
</city>
<city>
<id> 3 </id>
<name> 江苏 </name>
</city>
<city>
<id> 4 </id>
<name> 长沙 </name>
</city>
<city>
<id> 0 </id>
<name> 黑龙江 </name>
</city>
</city-list>
......

希望用 xsl 在网页中输出一个表格,比如需要输出成每行四列的表格。但是 xml 中的每个 city 都没有本质的区别,怎么控制它的列数啊?




[解决办法]
<?xml version= "1.0 " encoding= "gb2312 "?>
<xsl:stylesheet version= "1.0 " xmlns:xsl= "http://www.w3.org/1999/XSL/Transform ">
<xsl:template match= "/ ">
<table border= "1 ">
<xsl:apply-templates select= "city-list "/>
</table>
</xsl:template>
<xsl:template match= "root ">
<tr>
<xsl:apply-templates select= "//city " />
</tr>
</xsl:template>

<xsl:template match= "city ">
<td>
<xsl:value-of select= "name "/>
</td>
<!--换行-->
<xsl:if test= "not(position()=last()) and position() mod 4=0 ">
<xsl:text disable-output-escaping= "yes "> </tr><tr> </xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
city 都没有本质的区别,可以通过position()和mod来控制。
[解决办法]
try
-----------------------------
<?xml version= "1.0 " encoding= "GB2312 "?>
<HTML xmlns:xsl= "http://www.w3.org/TR/WD-xsl ">
<HEAD>
<TITLE> city </TITLE>
</HEAD>
<BODY>
<TABLE border= "1 " cellspacing= "0 ">
<tr>
<td> id </td>
<td> name </td>
</tr>

<xsl:for-each select= "city-list/city ">
<tr>
<td> <xsl:value-of select= "id "/> </td>
<td> <xsl:value-of select= "name "/> </td>
</tr>
</xsl:for-each>
</TABLE>

</BODY>
</HTML>


look
------------------------------------------------
http://www.blueidea.com/tech/web/2004/1797.asp

读书人网 >XML SOAP

热点推荐