You may want to insert a document fragment into an XML node. A document fragment is a piece of a Document Object Model (DOM) tree. Changes made to the fragment do not affect the document and are more efficient that updating the DOM directly. Typically, you make changes to the document fragment and then insert it into the DOM tree when ready.

The following sample program shows how to create a document fragment and insert it into the XML document. To create a document fragment, you call CREATE-NODE() and specify the "DOCUMENT-FRAGMENT" subtype.

DEFINE VARIABLE hDoc          AS HANDLE.
DEFINE VARIABLE hRoot         AS HANDLE.
DEFINE VARIABLE hRow          AS HANDLE.
DEFINE VARIABLE hText         AS HANDLE.
DEFINE VARIABLE hdoc_Frag     AS HANDLE.
DEFINE VARIABLE hInsert-Point AS HANDLE.

CREATE X-DOCUMENT hDoc.
CREATE X-NODEREF hRoot.
CREATE X-NODEREF hRow.
CREATE X-NODEREF hText. 
CREATE X-NODEREF hdoc_Frag. 
CREATE X-NODEREF hInsert-Point. 

/*set up a document 'Names' root node*/
hDoc:CREATE-NODE(hRoot,"Names","ELEMENT").
hDoc:APPEND-CHILD(hRoot).

/* Add several child 'name' nodes to the XML root */
RUN addName (INPUT hRoot, INPUT "Alice").
RUN addName (INPUT hRoot, INPUT "Bert").
RUN addName (INPUT hRoot, INPUT "Charlie").
RUN addName (INPUT hRoot, INPUT "Diane").
RUN addName (INPUT hRoot, INPUT "Eric").

/* Retreive document frgament from procedure */
RUN getBfragment (INPUT-OUTPUT hdoc_frag).

/* Insert the document fragment before node 3 in the XML document */
hRoot:GET-CHILD(hInsert-Point,3).
hRoot:INSERT-BEFORE(hdoc_frag,hInsert-Point).

/*write the XML node tree to an xml file*/
hDoc:SAVE("file","Names.xml").
DELETE OBJECT hDoc.
DELETE OBJECT hRoot.
DELETE OBJECT hRow.

PROCEDURE addName:
    /* Add name nodes to the XML */
    DEFINE INPUT PARAMETER h_Parent AS HANDLE NO-UNDO.
    DEFINE INPUT PARAMETER cName AS CHARACTER NO-UNDO.
  
    hDoc:CREATE-NODE(hRow,"Name","ELEMENT"). /*create a row node*/
    h_Parent:APPEND-CHILD(hRow). /*put the row in the tree*/

    hDoc:CREATE-NODE(hText,"","TEXT").
    hRow:APPEND-CHILD(hText).
    hText:NODE-VALUE = cName.
END PROCEDURE.

PROCEDURE getBfragment:
    DEFINE INPUT-OUTPUT PARAMETER out_frag AS HANDLE NO-UNDO.

    /* Create a document fragment that contains several other child 'name' nodes */
    hDoc:CREATE-NODE(out_frag,"Frag","DOCUMENT-FRAGMENT").
    hDoc:APPEND-CHILD(out_frag). /*put the row in the tree*/
    
    RUN addName (INPUT out_frag, INPUT "Billy").
    RUN addName (INPUT out_frag, INPUT "Bobby").
    RUN addName (INPUT out_frag, INPUT "Bonnie").
END PROCEDURE.