XForms:为一个循环插入所有列表项的子项

时间:2014-01-17 13:22:06

标签: orbeon xforms

如果我有像

这样的XForms实例
<xforms:instance id="myvrs">
    <myvrs>
        <subitem />
    </myvrs>
</xforms:instance>
<xforms:instance id="tmp">
    <temp>
        <item><subitem>11</subitem></item>
        <item><subitem>21</subitem><subitem>22</subitem></item>
        <item></item>
    </temp>
</xforms:instance>

如何使用像

这样的循环
<xforms:trigger>
    <xforms:label>button</xforms:label>
    <xforms:action events:event="DOMActivate">
    forall in instance('tmp')/item
        <xforms:insert context="instance('tmp')/item" origin="instance('myvrs')/subitem" />
    endforall
    </xforms:action>
</xforms:trigger>

我可以获得实例

<xforms:instance id="tmp">
    <temp>
        <item><subitem>11</subitem><subitem></subitem></item>
        <item><subitem>21</subitem><subitem>22</subitem><subitem></subitem></item>
        <item><subitem></subitem></item>
    </temp>
</xforms:instance>

谢谢

1 个答案:

答案 0 :(得分:3)

要在XForms 1.1中进行迭代,您需要使用while属性(请参阅10.18 Iteration of XForms Actions)。

在您的情况下可以使用其他实例:

<xf:instance id="iterator">
    <iterator xmlns=""></iterator>
</xf:instance>

然后行动可能是:

<xf:action ev:event="DOMActivate">
    <xf:setvalue ref="instance('iterator')" value="1"></xf:setvalue>
    <xf:action while="instance('iterator') &lt; count(instance('tmp')/item)">
        <xf:insert context="instance('tmp')/item[position()=instance('iterator')]"
            origin="instance('myvrs')/subitem" nodeset="subitem" position="after"></xf:insert>
        <xf:setvalue ref="instance('iterator')" value="instance('iterator') + 1"></xf:setvalue>
    </xf:action>
</xf:action>

请注意,这是标准的XForms 1.1,可以与其他实现一起使用。

完整的XForms将是:

<html xmlns:xf="http://www.w3.org/2002/xforms" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <head>
        <title>Insert for all</title>
        <xf:model>
            <xf:instance id="myvrs">
                <myvrs xmlns="">
                    <subitem></subitem>
                </myvrs>
            </xf:instance>
            <xf:instance id="tmp">
                <temp xmlns="">
                    <item>
                        <subitem>11</subitem>
                    </item>
                    <item>
                        <subitem>21</subitem>
                        <subitem>22</subitem>
                    </item>
                    <item></item>
                </temp>
            </xf:instance>
            <xf:instance id="iterator">
                <iterator xmlns=""></iterator>
            </xf:instance>
        </xf:model>
    </head>
    <body>
        <xf:trigger>
            <xf:label>button</xf:label>
            <xf:action ev:event="DOMActivate">
                <xf:setvalue ref="instance('iterator')" value="1"></xf:setvalue>
                <xf:action while="instance('iterator') &lt; count(instance('tmp')/item)">
                    <xf:insert context="instance('tmp')/item[position()=instance('iterator')]"
                        origin="instance('myvrs')/subitem" nodeset="subitem" position="after"></xf:insert>
                    <xf:setvalue ref="instance('iterator')" value="instance('iterator') + 1"></xf:setvalue>
                </xf:action>
            </xf:action>
        </xf:trigger>
        <fr:xforms-inspector xmlns:fr="http://orbeon.org/oxf/xml/form-runner"></fr:xforms-inspector>
    </body>
</html>