Search code examples
xforms

Adding attributes to instance elements in XForms


I have an instance that looks like the following:

<root>
    <switch ID="1">
        <foo/>
        <bar/>
    </switch>
    <switch ID="2">
        <foo/>
        <bar/>
    </switch>
</root>

I'm trying to create a trigger that adds an enabled attribute to a switch element. I've been using something like the following to add new child nodes to the switches, but as far as I can tell, this same method won't work to add only the attribute. This trigger is in a xf:repeat block, and goes away once the "bar" element has been added for the specific switch:

<xf:trigger ref=".[not(bar)]">
    <xf:label>Add "bar" Element</xf:label>
    <xf:insert ev:event="DOMActivate" context="../switch[index('switch-repeat')]" 
        nodeset="foo" at="foo" position="after" 
        origin="instance('presets')/bar"/>
</xf:trigger>

I've been thinking about copying the contents of the switch element into another instance with the enabled attribute, something like <switch enabled="true/>, and then replacing the original with that, but that seems a little bit roundabout for the task, and I might have to do the same thing to preserve the ID attributes anyway.


Solution

  • Guided by Alain's comment, I found this mention in the XForms standard for copying attribute lists into elements. This works exactly how I would've hoped.

    The code I'm using to add the attributes is the following:

    <xf:trigger ref=".[not(@enabled)]">
        <xf:label>Add "enabled" attribute</xf:label>
        <xf:insert ev:event="DOMActivate" context="../switch[index('switch-repeat')]"
            origin="instance('presets')/switch/@enabled"/>
    </xf:trigger>
    

    where instance('presets')/switch is <switch enabled="true"/>.