lr.xmlFind

Verifies that XML values are returned by a query.

JavaScript XML Functions (lr.xml)

Syntax

lr.xmlFind( {object} );

JavaScript Object

{
   xml:"<string>",
   query:"<string>", | fastQuery:"<string>",
   value:"<string>", | valueParam:"<string>",
   selectAll:"<string>",
   ignoreCase:"<string>",
   useRegExp:"<string>",
   notFound:"<string>"
} 
Property Name
Description
xmlThe XML Input String to query
queryThe XML Query on the input string xml.
fastQueryThe Fast Query on the input string xml.
valueThe string to find. This can be the element value or its attribute value.
valueParamThe parameter name containing the string to find.
selectAllIf "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.
ignoreCaseIf "yes", the search will ignore the difference between uppercase and lowercase characters of value or valueParam and query results. Default is "no".
useRegExpIf "yes", value or valueParam can be regular expressions that the function will search for. For more information, see Regular Expressions. The default is "UseRegExp=no".
notFoundSee Continuing on Error

The lr.xmlFind 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.xmlFind returns either 1 or 0.

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:

  • XML

  • Query

  • Value

  • XmlFragment

Example 1

The following example uses lr.xmlFind to check whether a certain employee exists.

function Action(){

   var xmlInput =
   "<acme_org>"+
       "<employee level=\"manager\">John Smith"+
       "<cubicle>227</cubicle>"+
       "</employee>"+
       "</acme_org>";

   var find_cnt;

       lr.saveString(xmlInput, "XML_Input_Param");

       /* Verify that employee John Smith exists */
       find_cnt = lr.xmlFind({xml:'{XML_Input_Param}',
        value : "John Smith", 
        query: "/acme_org/employee" });

       if (find_cnt >0)
   {
       /* 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"} 
        );

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

       } // end if find_cnt > 0

return 0;
}

Example 2

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.

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

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

For additional examples, see JavaScript XML query examples.