Search code examples
orbeonxformsxpath-2.0

How to insert nodes after specific node of an instance in Orbeon?


I have an instance in model:

<Exams>
  <ExamCode>14</ExamCode>
</Exams>
<Exams>
  <ExamCode>15</ExamCode>
</Exams>

I want to iterate on all elements and add after ExamCode element some nodes but without parent.

My instance after iterate should be like this:

<-- Wanted -->
<Exams>
  <ExamCode>14</ExamCode>
  <ExamTitle></ExamTitle>
  <ExamDescription>/ExamDescription>
</Exams>
<Exams>
  <ExamCode>15</ExamCode>
  <ExamTitle></ExamTitle>
  <ExamDescription></ExamDescription>
</Exams>

What i have tried is this:

Instance to add:

<!-- instance nodes to add-->
<xf:instance id="examRelatedNodes">
   <ExamTitle/>
   <ExamDescription/>
</xf:instance>

Adding "examRelatedNodes" instance to all repeated Exams nodes:

<!-- insert examRelatedNodes per Exam -->
<xf:action ev:event="xforms-ready" xxf:iterate="instance('fr-form-instance')/Exams">
   <xf:insert context="." origin="instance('examRelatedNodes')"/>
</xf:action>

But I get an error that instance to add, should have parent!

I have read the documentation, this from the orbeon blog, but in all paradigms i find, the elements of to-add-instance have a parent, but in my case i don't want to have.Following these examples with parent node, i am able to add them, BUT with parent node "ParentOfExtraNodes" that i don't want:

    <-- Not wanted -->
    <Exams>
     <ExamCode>14</ExamCode>
     <ParentOfExtraNodes> 
       <ExamTitle></ExamTitle>
       <ExamDescription>/ExamDescription>
      <ParentOfExtraNodes/> 
    </Exams>
    <Exams>
     <ExamCode>15</ExamCode>
     <ParentOfExtraNodes> 
       <ExamTitle></ExamTitle>
       <ExamDescription>/ExamDescription>
     <ParentOfExtraNodes/>
    </Exams>

I am using Orbeon Forms 4.5 (XForms implementation).


Solution

  • What you are doing looks about right. But in your instance examRelatedNodes, you need to have a root element, say:

    <xf:instance id="examRelatedNodes">
       <RelatedNodes>
           <ExamTitle/>
           <ExamDescription/>
       </RelatedNodes>
    </xf:instance>
    

    And then your insert will become:

    <xf:insert context="." origin="instance('examRelatedNodes')/*"/>