以文本方式查看主题

-  中文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=49729)


--  作者:haitian8080
--  发布时间:7/11/2007 9:24:00 PM

--  [求助]关于xsl的问题,高手门帮我解决一下!!!谢谢
我的xml:
<?xml version="1.0" encoding="utf-8"?>
<Bonus>
  <Dept>
      <DeptId>99901</DeptId>
      <Emplid>00001</Emplid>
      <DayBonus>100</DayBonus>
      <MonthBonus>1000</MonthBonus>
  </Dept>
  <Dept>
      <DeptId>99901</DeptId>
      <Emplid>00001</Emplid>
      <DayBonus>100</DayBonus>
      <MonthBonus>1000</MonthBonus>
  </Dept>
  <Dept>
   <DeptId>99901</DeptId>
      <Emplid>00001</Emplid>
      <DayBonus>100</DayBonus>
      <MonthBonus>1000</MonthBonus>
  </Dept>
  <Dept>
      <DeptId>99901</DeptId>
      <Emplid>00001</Emplid>
      <DayBonus>100</DayBonus>
      <MonthBonus>1000</MonthBonus>
  </Dept>
  <Dept>
      <DeptId>99901</DeptId>
      <Emplid>00001</Emplid>
      <DayBonus>100</DayBonus>
      <MonthBonus>1000</MonthBonus>
  </Dept>
  <Dept>
      <DeptId>99901</DeptId>
      <Emplid>00001</Emplid>
      <DayBonus>100</DayBonus>
      <MonthBonus>1000</MonthBonus>
  </Dept>
</Bonus>


我的xsl:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/">
    <html>
    <body>
      <table cellpadding="0" cellspacing="0" border="1" style="border-collapse:collapse;font-size:14px;">
        <tr>
          <th>DeptId</th>
          <th>Emplid</th>
          <th>DayBonus</th>
          <th>MonthBonus</th>
        </tr>
        <xsl:for-each select="Bonus/Dept[not(DeptId=preceding::*/DeptId)]">
        
          <tr>   
            <td>
              <xsl:value-of select="DeptId"></xsl:value-of>
            </td>
            <td>
              <xsl:value-of select="Emplid"></xsl:value-of>
            </td>
            <td>
              <xsl:value-of select="DayBonus"></xsl:value-of>
            </td>
            <td>
              <xsl:value-of select="MonthBonus"></xsl:value-of>
            </td>          
             </tr>
          <tr>
          <td></td>
          <td></td>
          <td><xsl:value-of select="sum(../Dept[DeptId=current()/DeptId]/DayBonus)"/>
            </td>
            <td><xsl:value-of select="sum(../Dept[DeptId=current()/DeptId]/MonthBonus)"/>
            </td></tr>
          </xsl:for-each-group>
      </table>
    </body>
    </html>
</xsl:template>
</xsl:stylesheet>
可是上面的xml与xsl显示的结果是:

DeptId   Emplid   DayBonus   MonthBonus
99901    00001    100         1000
   奖金总计:       300         3000

99902    00001    100         1000
   奖金总计:       300         3000

需求要的结果:
DeptId   Emplid   DayBonus   MonthBonus
99901    00001    100         1000
99901    00002    100         1000
99901    00003    100         1000
   奖金总计:       300         3000

99902    00001    100         1000
99902    00002    100         1000
99902    00003    100         1000
   奖金总计:       300         3000


--  作者:haitian8080
--  发布时间:7/11/2007 9:54:00 PM

--  
for-each-group 是xsl 2.0 的新特性可以;但是在ERP中没发用2.0新特性 ;只能用1.0
--  作者:Qr
--  发布时间:7/12/2007 9:02:00 AM

--  
不知道LZ对XPath了解多少,你知道在你的代码(xsl:for-each)中 not 和 preceding::* 在此起什么作用吗?
1、去掉xsl:for-each中的过滤语句;
2、再改第2个tr,增一个xsl:if判断:
<xsl:if test="not(DeptId=following::*/DeptId)">
<tr>
<td></td>
<td></td>
<td><xsl:value-of select="sum(../Dept[DeptId=current()/DeptId]/DayBonus)"/>
</td>
<td><xsl:value-of select="sum(../Dept[DeptId=current()/DeptId]/MonthBonus)"/>
</td>
</tr>
</xsl:if>
运行结果:
DeptId Emplid DayBonus MonthBonus
99901 00001 100 1000
99901 00001 100 1000
99901 00001 100 1000
  300 3000
99902 00001 100 1000
99902 00001 100 1000
99902 00001 100 1000
  300 3000
正是你想要的。


但是,得么这个结果的前提似乎是DeptId相邻,如果参杂在一起,这个可能就得利用递归了,也许先按DeptId排序也可以吧。


--  作者:Qr
--  发布时间:7/12/2007 9:08:00 AM

--  
对了,如果DeptId次序打乱,可以先按DeptId排序。测试结果如上。
--  作者:haitian8080
--  发布时间:7/12/2007 10:48:00 AM

--  
谢谢高手了!!!!
--  作者:haitian8080
--  发布时间:7/12/2007 10:50:00 AM

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