Share this page

# Example: lr.xmlFind

The following example inserts an "extension" tag to an employee record. It first verifies, using lr.xmlFind, that the record for the employee John Smith exists in the XML string xml_input. If it doesn't exist, then lr.xmlFind 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.xmlFind, the script will continue. In these cases, the check for the number of matches will prevent the lr.xmlInsert from executing. For more options for handling error conditions, see lr.exit.

If the find operation succeeds, lr.xmlInsert 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.xmlFind, 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.saveString(xml_input, "XML_Input_Param");

        /* Verify that employee John Smith exists */

        find_cnt = lr.xmlFind("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.xmlInsert("XML={XML_Input_Param}", "ResultParam=Result",

            "XmlFragment=<extension>2145</extension>",

            "Query=/acme_org/employee",

            "Position=child", LAST );

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

        } // end if find_cnt > 0

    return 0;
    }

Example: Output:
Action.c(15): "lr.xmlFind" was successful, 1 match processed
Action.c(20): "lr.xmlInsert" 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.xmlFind(    "Xml={XML_Input_Param}",

             "Query=/acme_org/employee/@level",

            "Value=manager",

            LAST

        );

        lr.xmlFind(    "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.xmlFind" was successful, 1 match processed
Action.c(19): Error: no matches were found for the specified query [class:CLrXmlScriptFunc]
Action.c(19): Error: "lr.xmlFind" execution failed

Tips to improve your search results... VuGen Function Reference Version 25.1-25.3 Function Reference 25.1-25.3 All View Local Help Center View Online Help Center Version: