0

我有一个如下的 xml:

<table1>
    <row>
        <person>person1</person>
        <value>10</value>
    </row>
    <row>
        <person>person2</person>
        <value>20</value>
    </row>
    <row>
        <person>person1</person>
        <value>5</value>
    </row>
</table1>
<summaryTable>
    <row>
        <person>person1</person>
        <value_total/>
    </row>
    <row>
        <person>person2</person>
        <value_total/>
    </row>
</summaryTable>

使用 XForms 1(没有切换到 XForms 2 的选项),使用框架 Betterform,我想通过对“table1”中具有相同人名的行求和来计算汇总表中的值。为此,我有以下绑定:

<xf:bind id="bind_table1"
    nodeset="table1" repeatableElement="row">
    <xf:bind id="bind_head_table1" nodeset="head" />
    <xf:bind id="bind_row_table1" nodeset="row">
        <xf:bind id="bind_person" nodeset="person"  type="xf:string" />
        <xf:bind id="bind_value" nodeset="value"    type="xf:integer" />
    </xf:bind>
</xf:bind>
<xf:bind id="bind_summaryTable"
    nodeset="summaryTable"
    repeatableElement="row">
    <xf:bind id="bind_head_summaryTable" nodeset="head" />
    <xf:bind id="bind_row_summaryTable" nodeset="row">
        <xf:bind id="bind_person_name" nodeset="person_name" type="xf:string" readonly="true"/>
        <xf:bind id="bind_value_total" nodeset="value_total" type="xf:integer" readonly="true" calculate="SUM(//table1/row[person/text() = ../person_name/text()]/value)"/>
    </xf:bind>
</xf:bind>

最后我想要的是 person1 = 15 的 value_total 和 person2 = 20 的 value_total,但是使用这个“计算”表达式我得到了“NaN”。如果我替换计算表达式以与文字字符串进行比较,例如:

<xf:bind id="bind_value_total" nodeset="value_total" type="xf:integer" readonly="true" calculate="SUM(//table1/row[person/text() = 'person1']/value)"/>

然后我得到 value_total 15 (总和正确完成)。所以似乎错误出在比较表达式 person/text() = ../person_name/text() 中。有人知道正确的表达方式应该是怎样的吗?

谢谢

4

1 回答 1

0

Try the context() function in the calculate attribute to refer to the current node, like this:

<xf:bind nodeset="summaryTable/row/value_total" calculate="sum(//table1/row[person/text() = context()/../person/text()]/value)"/>

The context function gives you the current context node. If your bind references a nodeset with multiple nodes, it will be evaluated one time for every node, and that node is what context() returns.

It works for me with XSLTForms, maybe your version of betterForm supports it.

于 2014-11-27T22:20:50.753 回答