Examples: lr_xml_get_values

For more examples of the use of lr_xml_get_values, see Example 1 - Executing queries and Value related functions in Example Scripts for XML Queries.

Example 1

The following example uses lr_xml_get_values to search for the name of an employee.

First, a simple XML string is stored in parameter XML_Input_Param. Then, lr_xml_get_values is invoked with the XPath query string "/employee/name". This query is then processed on the XML_Input_Param parameter string. The result of the query is stored in the OutputParam parameter. The value of OutputParam is evaluated using lr_eval_string.

#include "as_web.h"
char *xml_input=
     "<employee>"
          "<name>John Smith</name>"
          "<cubicle>227</cubicle>"
     "</employee>";

Action() {

     lr_save_string(xml_input, "XML_Input_Param"); // Save input as parameter

     lr_xml_get_values("XML={XML_Input_Param}",
          "ValueParam=OutputParam",
          "Query=/employee/name",
          LAST );

     lr_output_message(lr_eval_string("Query result = {OutputParam}"));

     return 0;
}

Example: Output:
Action.c(13): "lr_xml_get_values" was successful, 1 match processed
Action.c(18): Query result = John Smith

Example 2

The following example is similar to Example 1 except that multiple values are retrieved. The query retrieves all telephone extensions from the XML input string, xml_input.

#include "as_web.h"

char * xml_input =
"<acme_org>"
     " <accounts_dept>"
          "<employee>"
               " <name>John Smith</name>"
               "<cubicle>227</cubicle>"
               "<extension>2145</extension>"
          "</employee>"
     "</accounts_dept>"
     "<engineering_dept>"
          "<employee>"
               "<name>Sue Jones</name>"
               "<extension>2375</extension>"
          "</employee>"
     "</engineering_dept>"
"</acme_org>";

Action() {

     int i, NumOfValues;
     char buf[64];

     lr_save_string(xml_input, "XML_Input_Param"); // Save input as parameter

     NumOfValues= lr_xml_get_values("XML={XML_Input_Param}",
          "ValueParam=OutputParam",
          "Query=/acme_org/*/employee/extension",
          "SelectAll=yes", LAST );

     for ( i = 0; i < NumOfValues; i++) { /* Print multiple values of OutputParam */

          sprintf (buf, "Retrieved value %d : {OutputParam_%d}", i+1, i+1);
          lr_output_message(lr_eval_string(buf));
     }

     return 0;
}

Example: Output:
Action.c(34): Retrieved value 1 : 2145
Action.c(34): Retrieved value 2 : 2375