现在我一段xml,如何把root/ss下所有的a*b的值累加起来呢?
<root>
<ss>
<a>5</a>
<b>2</b>
</ss>
<ss>
<a>2</a>
<b>3</b>
</ss><ss>
<a>1</a>
<b>4</b>
</ss></root>我自己是这样写了,实现不了<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy� -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="root">
 
<xsl:template name="weight">
    <xsl:param name="totalWeight" select ="0"/>
</xsl:template>
<xsl:template name="currentWeight">
<xsl:call-template name="weight">
<xsl:variable name = "total">
<xsl:for-each select="ss">
  <xsl:value-of select="$totalWeight+ (a*b)" />  
   
</xsl:for-each> </xsl:variable></xsl:call-template>
</xsl:template>
    
</xsl:template>
</xsl:stylesheet>
求高手指点,急救啊。

解决方案 »

  1.   

    算了,看来我比你着急,下面是xsl 和 DTD
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="root/ss">
    <html>
    <head>
    <style type="text/css">
      h3 {font-size:12px; font-family:Times New Roman;}
    </style></head>
    <body><h3>A:<xsl:value-of select="a"/></h3>
    <h3>B:<xsl:value-of select="b"/></h3>
    <h3>A+B:<xsl:value-of select="(a+b)"/></h3></body>
     </html>
    </xsl:template>
    </xsl:stylesheet><!ELEMENT a (#PCDATA)><!ELEMENT b (#PCDATA)><!ELEMENT ss (a+, b+)><!ELEMENT root ( ss )+ >
      

  2.   

    没有DTD,就直接通过xslt/xsl来实现
    我是要实现循环累加,如果单独的加,就<xsl:value-of select="a+b"/>就可以了。。
    不好意思,我现在才发现我上面的做法贴错了。<?xml version="1.0" encoding="ISO-8859-1"?>
    <!-- Edited by XMLSpy� -->
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="root"><xsl:variable name = "total">
    <xsl:for-each select="ss">
    <xsl:value-of select="a*b" /> 
    </xsl:for-each>
    </xsl:variable><xsl:value-of select="sum($total)"/></xsl:template>
    </xsl:stylesheet>
    不过这种方法还是错的
      

  3.   

    唉,看来没人回我,自己做出来了.贴出来给你们分享一下。<?xml version="1.0" encoding="UTF-8" ?>
    <!-- New document created with EditiX at Wed Mar 24 11:03:12 CST 2010 -->
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text" indent="yes"/>
     <xsl:template match="root">
      <xsl:variable name="TotalSum">
       <xsl:call-template name="calculator">
        <xsl:with-param name="currSum">0</xsl:with-param>
        <xsl:with-param name="count">
         <xsl:value-of select="count(ss)"/>
        </xsl:with-param>
       </xsl:call-template>
      </xsl:variable><xsl:value-of select="$TotalSum"/>
     </xsl:template>
     <xsl:template name="calculator">
      <xsl:param name="currSum"/>
      <xsl:param name="count"/>
      <xsl:variable name="aa" select="number(ss[number($count)]/a)"/>
      <xsl:variable name="bb" select="number(ss[number($count)]/b)"/>
      <xsl:variable name="sum" select="number($aa* $bb)"/>
      <xsl:variable name="CycleSum" select="number($currSum + $sum)"/>
      <xsl:choose>
       <xsl:when test="number($count - 1) &gt; 0 ">
        <xsl:call-template name="calculator">
         <xsl:with-param name="currSum">
          <xsl:value-of select="$CycleSum"/>
         </xsl:with-param>
         <xsl:with-param name="count">
          <xsl:value-of select="number($count - 1)"/>
         </xsl:with-param>
        </xsl:call-template>
       </xsl:when>
       <xsl:otherwise>
        <xsl:value-of select="$CycleSum"/>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:template>
    </xsl:stylesheet>