xslt 如何实现累加

lovely_autumn 2010-03-23 07:59:22
现在我一段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>


求高手指点,急救啊。。。。
...全文
314 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovely_autumn 2010-03-25
  • 打赏
  • 举报
回复
唉,看来没人回我,自己做出来了.贴出来给你们分享一下。。。。

<?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) > 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>


lovely_autumn 2010-03-23
  • 打赏
  • 举报
回复
没有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>


不过这种方法还是错的。。。
clinique1234 2010-03-23
  • 打赏
  • 举报
回复
算了,看来我比你着急,下面是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 )+ >
clinique1234 2010-03-23
  • 打赏
  • 举报
回复
你的DTD呢???

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧