Example: lr_xml_find

The following example inserts an "extension" tag to an employee record. It first verifies, using lr_xml_find, that the record for the employee John Smith exists in the XML string xml_input. If it doesn't exist, then lr_xml_find will terminate execution of the script and send an error message.

However, if Continue on error is selected in the runtime settings or "NotFound =continue" is passed to lr_xml_find, the script will continue. In these cases, the check for the number of matches will prevent the lr_xml_insert from executing. For more options for handling error conditions, see lr_exit.

If the find operation succeeds, lr_xml_insert searches xml_input for the tag "<employee>" and inserts an XML fragment which contains the extension information. ("XmlFragment=<extension>2145</extension>").

The resulting string is contained in the ResultParam parameter, Result.

For more examples of the use of lr_xml_find, see Finding the first occurrence, Try to find a partial string, Finding a partial string with regular expressions, Finding a complete string that is not the first occurrence, and Specifying the occurrence in the Verify the presence of values in the XML string section of Example Scripts for XML Queries.

#include "as_web.h"

char *xml_input =
"<acme_org>"
    "<employee level=\"manager\">John Smith"
        "<cubicle>227</cubicle>"
    "</employee>"
"</acme_org>";

Action() {
int find_cnt;
    lr_save_string(xml_input, "XML_Input_Param");
    /* Verify that employee John Smith exists */
    find_cnt = lr_xml_find("XML={XML_Input_Param}",
        "Value=John Smith", 
        "Query=/acme_org/employee", 
        LAST );
    if (find_cnt >0)
{
        /* Now insert John Smith's telephone extension number */
        lr_xml_insert("XML={XML_Input_Param}", "ResultParam=Result",
        "XmlFragment=<extension>2145</extension>", 
        "Query=/acme_org/employee",
        "Position=child", LAST );
        lr_output_message(lr_eval_string("String after insertion: {Result}"));
    } // end if find_cnt > 0 
return 0;
}

Example: Output:
Action.c(15): "lr_xml_find" was successful, 1 match processed
Action.c(20): "lr_xml_insert" was successful, 1 match processed
Action.c(25): String after insertion: <acme_org><employee>John Smith<cubicle>227</cubicle><extension>2145</extension></employee></acme_org>

Example 2

The following example searches by the value of the level attribute. In the first instance, the text manager was found. In the second instance, no match was found for director.

    lr_xml_find(    "Xml={XML_Input_Param}",
         "Query=/acme_org/employee/@level",
        "Value=manager",
        LAST
    );
    lr_xml_find(    "Xml={XML_Input_Param}",
         "Query=/acme_org/employee/@level",
        "Value=director",
        LAST
    );

Example: Output (in the Execution log, or the output.txt file):
Action.c(14): "lr_xml_find" was successful, 1 match processed
Action.c(19): Error: no matches were found for the specified query [class:CLrXmlScriptFunc]
Action.c(19): Error: "lr_xml_find" execution failed