lr_xml_replace

XML Functions (lr_xml)

Replaces fragments of an XML string.

int lr_xml_replace( <List of specifications> [, <List of optional specifications> ] [, LAST]);
List of specifications

For the following list of required specifications, use the following string format:"Specification=value"

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.

Choose one of the following:
XmlFragment: the string value to use as replacement of the query match—an element or an attribute.
or:
XmlFragmentParam: the name of the parameter containing the string value to use as replacement

List of optional specifications

For the following list of optional specifications, use the following string format:"Specification=value"

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 Continue on Error

LAST A marker which indicates the end of the List of optional specifications

The lr_xml_replace 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_xml_replace 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_xml_set_values.

Return Values

See Return Values

Parameterization

See Parameterization

Example

The following example changes the telephone extension number of an employee from 2145 to 4444. It searches the input string xml_input for the tag "<extension>" and replaces the tag with an XML fragment containing the number 4444 ("XmlFragment=<extension>4444</extension>"). The resulting string is contained in the ResultParam parameter, Result.

For more examples of the use of lr_xml_replace, see Examples: common actions.

#include "as_web.h"

char *xml_input =
"<acme_org>"
     "<employee>"
          " <name>John Smith</name>"
          "<cubicle>227</cubicle>"
          "<extension>2145</extension>"
     "</employee>"
"</acme_org>";

Action() {

     lr_save_string(xml_input, "XML_Input_Param");

     lr_xml_replace("XML={XML_Input_Param}", "ResultParam=Result",
               "Query=/acme_org/employee/extension",
               "XmlFragment=<extension>4444</extension>", LAST );

     lr_output_message(lr_eval_string("String after replacement: {Result}"));

     return 0;
}

Example: Output:
Action.c(20): String after replacement: <acme_org><employee> <name>John Smith</name><cubicle>227</cubicle> <extension>4444</extension></employee></acme_org>