Inserts a new XML fragment into an XML string.

ExampleXML Functions (LR_XML)


lr.xmlInsert( {object} );

JavaScript Object

   xmlFragment:"<string>", | xmlFragmentParam:"<string>",
Property Name
xmlThe XML Input String to query.
resultParamThe name of the output parameter containing the XML data after replacing the new value.
queryThe XML Query on the input string XML.
xmlFragmentThe string value to use as replacement of the query match—an element or an attribute.
xmlFragmentParamThe name of the parameter containing the string value to use as replacement.
selectAllIf "yes", all elements matching the query will be processed. If "no", only the first match will be processed. Default is "no". See Multiple Query Matching.
positionThe position to insert the XML fragment. One of:
  • before: Place the fragment before the tag returned by a query.
  • after: Place the fragment after the tag returned by a query (default)
  • child: Place the fragment as a child of the tag by a query.
  • attribute: Indicates an attribute of an element returned by a query.
notFound See Continuing on Error.

The lr.xmlInsert function queries the XML input string XML for values matching the Query criteria. It then inserts XmlFragment or XmlFragmentParam at the position (or positions) in the XML string returned by the Query.

Position specifies whether the insertion is done before or after the point returned. Additionally, the childPosition specifies that the fragment is inserted before the end of the tag found by the query. For example, if the input string is


an inserted fragment ("<b>ZZ</b>") in the child position will result in the string:


The resulting string after insertion is placed in ResultParam, using the source document encoding. The output preserves the XML Character Encoding of the original document, independent of the input encoding. The input encoding uses the client's local encoding.

Return Values

All XML functions return the number of matches successfully found or zero on failure.


Input parameters to XML functions with the following names can be parameterized:

  • XML

  • Query

  • Value

  • XmlFragment


function Action(){
   var xmlInput =
       "<employee level=\"manager\">John Smith"+

   var findCnt;

   lr.saveString(xmlInput, "XML_Input_Param");

   /* Verify that employee John Smith exists */
   findCnt = lr.xmlFind({xml:'{XML_Input_Param}',
      value : "John Smith", 
      query: "/acme_org/employee" });

   if (findCnt > 0)
      /* Insert John Smith's telephone extension number */
            resultParam :"Result",
            xmlFragment: "<extension>2145</extension>", 
            query :"/acme_org/employee",
            position: "child"} );

       lr.outputMessage(lr.evalString("String after insertion: {Result}"));

return 0;