Share this page

# lr_xml_find

XML Functions (lr_xml)

Verifies that XML values are returned by a query.

    int lr_xml_find( <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" Query: the XML Query or Fast Query on the input string XML. XML: the XML Input String to query Choose one of the following: Value: the string to find. This can be the element value or its attribute value. or: ValueParam: the parameter name containing the string to find.
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 query match will be verified. Default is "no". See Multiple Query Matching IgnoreCase: If "yes", the search will ignore the difference between uppercase and lowercase characters of Value or ValueParam and query results. Default is "no". UseRegExp: If "yes", Value and ValueParam can be regular expressions that the function will search for. For more information, see Regular Expressions. The default is "UseRegExp=no". NotFound: Specifies whether the script fails or continues if the search value is not found. Value is "continue" or "error". If NotFound is not specified, the script fails.
LAST | A marker which indicates the end of the List of optional specifications

The lr_xml_find function queries the XML input string XML for values matching the Query criteria (Value or ValueParam) and returns the number of occurrences. If SelectAll is "no", lr_xml_find returns either 1 or 0.

## Return Values

See Return Values

## Parameterization

See Parameterization

## Example

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 Examples: common actions.

Copy code

    #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.

Copy code

        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

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