答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()&lt;$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()&lt;$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 &gt; (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()&lt;$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) &gt; 2*$Rows) and not($number &gt; ((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) &gt; $Rows) and (count(//movie) &lt; 2*$Rows) and not($number &gt; (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 &gt; count(//movie)) and not($number &gt; ($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>...来改写,甚至不用判断语句而重写该部分代码。。。

Qr

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,呵呵。

Kinogam

Posted by Kinogam on 2009/1/6 10:43:57

回复:答CSDN网友:表格自动换行(不够一行的用零填满)

2009/1/4 10:49:49

我觉得假如要显示到web上,不够的还是用&nbsp;吧 以下为blog主人的回复:  按需吧

Kinogam

Posted by Kinogam on 2009/1/4 10:49:49

回复:答CSDN网友:表格自动换行(不够一行的用零填满)

2009/1/4 10:47:45

在函数template里不能调用position比较麻烦只能靠传参数,哈哈,不过总的来说现在我习惯用xsl:include了……弄个文件把这些都按功能放…… 以下为blog主人的回复:  xsl:include?这个东东偶用得比较少,不明白你什么意思?写个例子给偶吧。

Kinogam

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主人的回复: 最近比较忙,少有时间写代码,只有随便捡些平时的贴子贴上来充数了。

hjx_221

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

» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144752920 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号