Summary table with xForms

113 Views Asked by At

I have an xml like the following:

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

With XForms 1 (there is no option to switch to XForms 2), using framework betterform, I want to calculate the values in the summary table, by doing the SUM of the rows in 'table1' that have the same person name. To do that I have the following binds:

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

What I want to have at the end is the value_total for person1 = 15 and value_total for person2 = 20, but using this 'calculate' expression I'm getting 'NaN'. If I replace the calculate expression to compare with a literal String like:

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

then I get as value_total 15 (the sum is correctly done). So it seems that the error is in the comparison expression person/text() = ../person_name/text() . Does someone have an idea about how should be the correct expression?

Thanks

1

There are 1 best solutions below

1
On

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.