以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  [求助]急:也是XSL/XSLT表格转换问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=71078)


--  作者:supershc
--  发布时间:1/1/2009 1:56:00 PM

--  [求助]急:也是XSL/XSLT表格转换问题
刚刚开始学习XML。想通过XSL/XSLT转换下面这段XML代码成为:
1. HTML的表格形式。
2. CSV格式的文件

<root>
   <row>
      <elem name="course">xml</elem>
      <elem name="date">mon</elem>
      <elem name="hour">14h16h</elem>
   </row>
   <row>
      <elem name="course">network</elem>
      <elem name="date">tue</elem>
      <elem name="hour">10h12h</elem>
   </row>
   <row>
      <elem name="course">java</elem>
      <elem name="date">wen</elem>
      <elem name="hour">14h16h</elem>
   </row>
...
...
...
   <row>
   </row>
</root>


写了一个XSLT文件转换表格的,结果和想的不一样。
部分XSLT代码如下:
<xsl:template match="root">
<p>
 <xsl:if test="position( )=1">
  <table border="1">
   <thead>
    <tr>
     <td>course</td>
     <td>Date</td>
     <td>Hour</td>
    </tr>
   </thead>
       
   <tbody>
    <tr>
     <td>
      <xsl:for-each select="row">
        <tr>
        <xsl:for-each select=".">
         <xsl:value-of select="elem" />
        </xsl:for-each>
       </tr>
      </xsl:for-each>
     </td>
    </tr>    
   </tbody>
  </table>
 </xsl:if>
</p>
</xsl:template>


现在问题是,其中的
<xsl:for-each select="row">
  <tr>
  <xsl:for-each select=".">
   <xsl:value-of select="elem" />
  </xsl:for-each>
 </tr>
</xsl:for-each>
只能显示每个row的第一个elem的name,也就是:
course    |    Date    |    Hour
---------------------------------------------
XML       |
network   |
java       |


希望格式:
course    |    Date    |    Hour
---------------------------------------------
XML        |    mon    |   14h16h
---------------------------------------------
...
...
...

请问该如何修改?
再就是CSV格式应该怎样写?
实在是“焦头乱额”,马上就要用了,谢谢各位先。


--  作者:supershc
--  发布时间:1/4/2009 4:19:00 AM

--  
大家帮忙看一下 :)
--  作者:wb1125
--  发布时间:1/4/2009 6:23:00 PM

--  
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:template match="/">
  <html>
   <head>
    <title></title>
   </head>
   <body>
    <table border="1">
     <tbody>
      <tr>
       <td>Course</td>
       <td>Date</td>
       <td>Hour</td>
      </tr>
      <xsl:for-each select="root/row">
      <tr>
       <td><xsl:value-of select="elem[@name='course']"></xsl:value-of></td>
       <td><xsl:value-of select="elem[@name='date']"></xsl:value-of></td>
       <td><xsl:value-of select="elem[@name='hour']"></xsl:value-of></td>
      </tr>
      </xsl:for-each>
     </tbody>
    </table>
   </body>
  </html>
 </xsl:template>
</xsl:stylesheet>
--  作者:supershc
--  发布时间:1/5/2009 4:42:00 AM

--  
谢谢wb1125的帮助。原来是可以这样的。

对于 CSV,写了些东西。
<xsl:template match="/">
 <xsl:for-each select="root">
  <xsl:for-each select="row[1]">
   <xsl:for-each select="elem"><xsl:value-of select="@name" />,</xsl:for-each>
   <xsl:text> </xsl:text>
  </xsl:for-each>
  
  <xsl:for-each select="row">
   <xsl:for-each select="elem"><xsl:value-of select="." />,</xsl:for-each>
   <xsl:text> </xsl:text>
  </xsl:for-each>
 </xsl:for-each>
</xsl:template>


生成的文件是这样的:
course,date,hour,
xml,mon,14h16h,
network,tue,10h12h,
java,wen,14h16h,
每行都多了一个逗号,该怎样去掉?谢谢。


--  作者:wb1125
--  发布时间:1/5/2009 10:35:00 AM

--  
这个逗号可以加判断来实现,在xsl中可以后<xsl:choose><xsl:when></xsl:when><xsl:ohterwise></xsl:otherwise></xsl:choose>来实现如:
<xsl:for-each select="row">
   <xsl:for-each select="elem">
   <xsl:choose>
    <xsl:when test="position()=3">
     <xsl:value-of select="."></xsl:value-of>
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="."></xsl:value-of>,
    </xsl:otherwise>
 </xsl:choose>
   </xsl:for-each>
   <xsl:text> </xsl:text>
  </xsl:for-each>
--  作者:supershc
--  发布时间:1/6/2009 9:39:00 AM

--  
再次感谢 wb1125。
通过位置判断就可以解决,自己怎末没有想到
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.500ms