答CSDN网友:表格自动换行(不够一行的用零填满) 
2008/12/29 15:50:27
阅读全文(9081) | 回复(6) | 编辑 | 精华
这个贴子不能说有新意,但是似乎万变不离其中,干脆贴在BLOG上,供大家参考吧。 原贴链接:http://topic.csdn.net/u/20081229/11/958c8752-091d-4385-9cfa-4b239e3721ca.html?seed=966865915 P.S.:Qiaorui是我在CSDN上的网名。 问: XML code <root> <movie>1</movie> <movie>2</movie> <movie>3</movie> <movie>4</movie> <movie>5</movie> <movie>6</movie> <movie>7</movie> <movie>8</movie> <movie>9</movie> <movie>10</movie> <movie>11</movie> <movie>12</movie> </root> <?xml version="1.0" encoding="GB2312"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="Rows">5</xsl:variable> <xsl:template match="//root"> <table> <xsl:for-each select="movie[position() mod $Rows=1]"> <tr> <xsl:apply-templates select=".|following-sibling::*[position()<$Rows]"/> </tr> </xsl:for-each> </table> </xsl:template> <xsl:template match="movie"> <td> <xsl:value-of select="."/> </td> </xsl:template> </xsl:stylesheet> 实现数据显示在一个表格,并且自动换行,不够一行的,要用零填满。 1 2 3 4 5 6 7 8 9 10 11 12 0 0 0 答: <?xml version="1.0" encoding="GB2312"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="Rows">5 </xsl:variable> <xsl:template match="//root"> <table border="1"> <xsl:for-each select="movie[position() mod $Rows=1]"> <tr> <xsl:apply-templates select=".|following-sibling::*[position()<$Rows]"/> <xsl:if test="position()=last() and not(count(//movie) mod $Rows=0)"> <xsl:call-template name="numtemplate"> <xsl:with-param name="number" select="1"/> </xsl:call-template> </xsl:if> </tr> </xsl:for-each> </table> </xsl:template> <xsl:template match="movie"> <td> <xsl:value-of select="."/> </td> </xsl:template> <xsl:template name="numtemplate"> <xsl:param name="number"/> <td>0 </td> <xsl:if test="not($number > (count(//movie) mod $Rows))"> <xsl:call-template name="numtemplate"> <xsl:with-param name="number" select="$number + 1"/> </xsl:call-template> </xsl:if> </xsl:template> </xsl:stylesheet> 红色为所增加的部分. 修正: <?xml version="1.0" encoding="GB2312"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="Rows">2 </xsl:variable> <xsl:template match="//root"> <table border="1"> <xsl:for-each select="movie[position() mod $Rows=1]"> <tr> <xsl:apply-templates select=".|following-sibling::*[position()<$Rows]"/> <xsl:if test="position()=last() and not(count(//movie) mod $Rows=0)"> <xsl:call-template name="numtemplate"> <xsl:with-param name="number" select="1"/> </xsl:call-template> </xsl:if> </tr> </xsl:for-each> </table> </xsl:template> <xsl:template match="movie"> <td> <xsl:value-of select="."/> </td> </xsl:template> <xsl:template name="numtemplate"> <xsl:param name="number"/> <xsl:if test="(count(//movie) > 2*$Rows) and not($number > ((count(//movie) mod $Rows)+1))"> <td>0 </td> <xsl:call-template name="numtemplate"> <xsl:with-param name="number" select="$number + 1"/> </xsl:call-template> </xsl:if> <xsl:if test="(count(//movie) > $Rows) and (count(//movie) < 2*$Rows) and not($number > (2*$Rows - count(//movie)))"> <td>0 </td> <xsl:call-template name="numtemplate"> <xsl:with-param name="number" select="$number + 1"/> </xsl:call-template> </xsl:if> <xsl:if test="($Rows > count(//movie)) and not($number > ($Rows - count(//movie)))"> <td>0 </td> <xsl:call-template name="numtemplate"> <xsl:with-param name="number" select="$number + 1"/> </xsl:call-template> </xsl:if> </xsl:template> </xsl:stylesheet> Rows=1时不显示,这个大家自己考虑。 <xsl:if>可以用 <xsl:choose> <xsl:when>...来改写,甚至不用判断语句而重写该部分代码。。。
Posted by Qr on 2008/12/29 15:50:27
回复:答CSDN网友:表格自动换行(不够一行的用零填满)
2009/1/6 10:43:57
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
xsl:include的例子么……以前写的找不到了…… 主要是把一些类似函数功能的xsl:template按功能放到一个xslt文件里面,然后方便日后重用。用xsl:include直接调用那个文件,就可以在页面上call了。感觉有点像普通的程序开发了…… 当然xslt1.0还是能让主xslt页面简洁起来的。 以下为blog主人的回复: 晕了头了,这个偶也写过,只是前日把xsl:include误当作xi:include,呵呵。
Posted by Kinogam on 2009/1/6 10:43:57
回复:答CSDN网友:表格自动换行(不够一行的用零填满)
2009/1/4 10:49:49
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
我觉得假如要显示到web上,不够的还是用 吧 以下为blog主人的回复: 按需吧
Posted by Kinogam on 2009/1/4 10:49:49
回复:答CSDN网友:表格自动换行(不够一行的用零填满)
2009/1/4 10:47:45
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
在函数template里不能调用position比较麻烦只能靠传参数,哈哈,不过总的来说现在我习惯用xsl:include了……弄个文件把这些都按功能放…… 以下为blog主人的回复: xsl:include?这个东东偶用得比较少,不明白你什么意思?写个例子给偶吧。
Posted by Kinogam on 2009/1/4 10:47:45
回复:答CSDN网友:表格自动换行(不够一行的用零填满)
2008/12/30 18:10:27
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
哈哈哈,不懂装懂啊.提前祝你新年66大顺! 以下为blog主人的回复: 谢谢,也祝你新年快乐!
Posted by 烟雨朦胧 on 2008/12/30 18:10:27
回复:答CSDN网友:表格自动换行(不够一行的用零填满)
2008/12/30 14:46:22
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
学习学习!! 谢谢! 以下为blog主人的回复: 最近比较忙,少有时间写代码,只有随便捡些平时的贴子贴上来充数了。
Posted by hjx_221 on 2008/12/30 14:46:22
回复:答CSDN网友:表格自动换行(不够一行的用零填满)
2008/12/30 8:31:17
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
受益匪浅啊. 以下为blog主人的回复: 受益匪浅?难道你也学起XML来?才不信呢
Posted by 烟雨朦胧 on 2008/12/30 8:31:17
发表评论: |