JavaScript Functions > Alphabetical Listing of Utility Functions (lr) > lr.xmlReplace

lr.xmlReplace

Replaces fragments of an XML string.

Example XML Functions (LR_XML)

Syntax

lr.xmlReplace( {object} );

JavaScript Object

{
   xml:"<string>",
   resultParam:"<string>",
   query:"<string>",
   xmlFragment:"<string>", | xmlFragmentParam:"<string>", 
   selectAll:"<string>",
   notFound:"<string>"
} 
Property Name
Description
xml The XML Input String to query.
resultParam The name of the output parameter containing the XML data after replacing the new value.
query The XML Query on the input string XML.
xmlFragment The value to use as replacement of the query match—an element or an attribute.
xmlFragmentParam The name of the parameter containing the string value to use as replacement.
selectAll If "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.
notFound See Continuing on Error.

The lr.xmlReplace function queries the XML input string XML for values matching the Query criteria and replaces them either with XmlFragment or XmlFragmentParam as the value of the elements matched by the query. You can replace elements by specifying either its element name or attribute in the XML Query. The resulting string 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.

If there is more than one value to be replaced, then pass the "XmlFragmentParam=" specification. Save the values in a series of parameters with the names:

Param_1, Param_2, Param_3, ...

where Param is the value "XmlFragmentParam=Param". lr.xmlReplace replaces each successive match of the Query with the value of the next parameter in the series. For an example of multiple-value query see lr.xmlSetValues.

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:

Example

function Action(){

   var xmlInput =
     "<acme_org>"+
     "<employee>"+
     " <name>John Smith</name>"+
     "<cubicle>227</cubicle>"+
     "<extension>2145</extension>"+
     "</employee>"+
     "</acme_org>";
   
   lr.saveString(xmlInput, "XML_Input_Param");
   lr.xmlReplace({xml:"{XML_Input_Param}", resultParam:"Result", 
       query:"/acme_org/employee/extension", 
       xmlFragment:"<extension>4444</extension>"});

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

return 0;
}