以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  xsl中关于计数的问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=39795)


--  作者:youngshuai
--  发布时间:11/8/2006 11:47:00 AM

--  xsl中关于计数的问题
xml文件如下
<books>
    <book>西游记</book>
    <book>水浒传</book>
    <book>三国演义</book>
</books>

想通过xsl转化为以下的html
<table>
   <tr>
       <td rowspan="3">书籍</td>
       <td>西游记</td>
   </tr>
   <tr>
        <td>水浒传</td>
   </tr>
   <tr>
       <td>三国演义</td>
  </tr>
</table>

我打算用<xsl:for-each>实现。但就是无法计算出<book>的数量,也不知道怎样将个数传给<td rowspan="count">.
<table>
   <xsl:foreach select="books/book">
   <tr>
       <td rowspan="<xsl:value-of select=\"count(.)\"/>">书籍</td>       <td><xsl:value-of select="."/></td>
   </tr>
</table>


--  作者:Qr
--  发布时间:11/8/2006 1:17:00 PM

--  
<?xml version="1.0" encoding="gb2312" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
 <table>
 <xsl:apply-templates select="books/book" />
 </table>
</xsl:template>


<xsl:template match="book">
 <tr>
  <xsl:if test="position()=1">
   <td rowspan="{count(../book)}">书籍</td>
  </xsl:if>
  <td><xsl:value-of select="."/></td>
 </tr>
</xsl:template>

</xsl:stylesheet>


不好意思,for-each:

<xsl:template match="/">
 <table>
 <xsl:for-each select="books/book">
   <tr>
 <xsl:if test="position()=1"><td rowspan="{count(../book)}">书籍</td></xsl:if>
 <td><xsl:value-of select="."/></td>
 </tr>
 </xsl:for-each>
 </table>
</xsl:template>


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms