lr.xmlInsert

Inserts a new XML fragment into an XML string.

ExampleXML Functions (LR_XML)

Syntax

lr.xmlInsert( {object} );

JavaScript Object

   xml:"<string>",
   resultParam:"<string>",
   query:"<string>",
   xmlFragment:"<string>", | xmlFragmentParam:"<string>",
   selectAll:"<string>",
   position:"<string>",
   notFound:"<string>"
} 
Property Name
Description
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

<a>53</a>

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

<a>53<b>ZZ</b></a>

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.

Parameterization

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

  • XML

  • Query

  • Value

  • XmlFragment

Example

function Action(){
   var xmlInput =
       "<acme_org>"+
       "<employee level=\"manager\">John Smith"+
       "<cubicle>227</cubicle>"+
       "</employee>"+
       "</acme_org>";

   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 */
      lr.xmlInsert({xml:"{XML_Input_Param}",
            resultParam :"Result",
            xmlFragment: "<extension>2145</extension>", 
            query :"/acme_org/employee",
            position: "child"} );
        

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

return 0;
}