一个需要累加的XSLT

数字蛋糕 2005-02-28 07:53:24
数据源为一个表格:

<rowset>
<row>
<f0>abc</f0>
<f1>1</f1>
<f2>1</f2>
<f3>1</f3>
<f4>123</f4>
<f5>321</f5>
</row>
<row>
<f0>abc</f0>
<f1>1</f1>
<f2>0</f2>
<f3>1</f3>
<f4>456</f4>
<f5>654</f5>
</row>
<row>
<f0>def</f0>
<f1>0</f1>
<f2>0</f2>
<f3>1</f3>
<f4>789</f4>
<f5>987</f5>
</row>
...
</rowset>

其中f0是某一外键(可能重复);逻辑型字段f1/f2/f3的值是0或1;而f4和f5是数值型。

现在需要的结果是输出主键为F0(必须不重复),输出的另一个字段为F2,其含义为:
F2 = SUM((f5 - f4) * (f1 + f2 + f3) | 对所有的f0 = F0)

因为XSLT使用无赋值语句模式造成这一算法的实现困难,但求一XSLT思路。
...全文
124 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2005-03-01
  • 打赏
  • 举报
回复
from

http://d0om.fnal.gov/d0admin/doctaur/dtdocs/p-langs/xml_bookshelf/xslt/ch01_01.htm

XSLT is heavily influenced by the design of functional programming languages, such as Lisp, Scheme, and Haskell. These languages also feature immutable variables. Instead of defining the templates of XSLT, functional programming languages define programs as a series of functions, each of which generates a well-defined output (free from side effects, of course) in response to a well-defined input. The goal is to execute the instructions of a given XSLT template without affecting the execution of any other XSLT template
数字蛋糕 2005-03-01
  • 打赏
  • 举报
回复
谢谢楼上的!这个问题我后来没多久也是通过递归的模板来解决的。

不过个人觉得无法体会所谓的“无赋值模式”到底有什么有意义的好处,能够让人放弃真正的“变量”?毕竟这跟当年的“无GOTO编程”不在同一个档次上。
数字蛋糕 2005-03-01
  • 打赏
  • 举报
回复
谢谢!
saucer 2005-03-01
  • 打赏
  • 举报
回复
use the Muenchian Method and a recursive template, see

http://www.jenitennison.com/xslt/grouping/muenchian.html

or try

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="RowF0" match="row" use="f0" />
<xsl:template match="rowset">
<xsl:for-each select="row[count(. | key('RowF0',f0)[1])=1]">
FO:<xsl:value-of select="f0" />
F2:<xsl:call-template name="calc">
<xsl:with-param name="rows" select="key('RowF0',f0)" />
<xsl:with-param name="sum" select="0" />
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="calc">
<xsl:param name="rows" />
<xsl:param name="sum" />
<xsl:choose>
<xsl:when test="$rows">
<xsl:call-template name="calc">
<xsl:with-param name="rows" select="$rows[position() > 1]" />
<xsl:with-param name="sum" select="$sum + ($rows[1]/f5 - $rows[1]/f4) * ($rows[1]/f1 + $rows[1]/f2 + $rows[1]/f3)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$sum" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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