Search code examples
xmlxsltorbeonxforms

XForms - copy child nodes from one instance to another


My problem is that I have a section in my main instance as such:

 <DocumentBody>
    :     :    :
    <SectionToBeUpdated />
    :     :    :
 </DocumentBody>

And also an updated "SectionToBeUpdated" in a different instance as such:

   <SectionToBeUpdated>
       <Section />
       <Section />
       <Section />
   </SectionToBeUpdated>

The behaviour I require is that all child elements of SectionToBeUpdated in the main instance are removed and replaced with the child elements of SectionToBeUpdated in the other instance.

Each time I try I get the whole SectionToBeUpdated section nested inside the existing SectionToBeUpdated section in the main instance.

I hope that's understandable? TIA


Solution

  • Assuming that your main instance id is main-instance, and your other instance with <SectionToBeUpdated> as the root element is section-template, the following actions should do it:

    <xf:delete
        ref="instance('main-instance')/SectionToBeUpdated/*"/>
    <xf:insert
        context="instance('main-instance')/SectionToBeUpdated"
        origin="instance('section-template')/SectionToBeUpdated/*"/>