Example Scripts for XML Queries

The scripts below can be run to illustrate XML Queries.

Example 1 - Executing queries demonstrates queries using lr_xml_get values. Example 2 - Examples of common activities demonstrates a wide variety of XML functions and expressions.

To run the examples:

  • Open Vugen

  • From the main menu choose File > New

  • Choose WebServices Vuser from the Protocol Selection. Click OK.

  • Click on the "Action" action in the left-hand frame. The Action page appears.

  • Copy the sample script and paste into the Action page, overwriting the code.

  • To see the results of the queries (the ResultParam value) click VuGen's menu item Vuser > RunTime Settings and select Log > Log Options > Extended Log and Parameter Substitution. The value will appear in VuGen's Execution Log window.

  • Run the Vuser

Example 1 - Executing queries

The function lr.xmlGetValues executes queries on the string xml_input. The results of each query are stored in the parameter Result.

#include "as_web.h"

/* The XML Data */
char *xml_input=
"<acme_org>"
     " <accounts_dept>"
          "<employee>"
               " <name>Kevin Sharp</name>"
               "<cubicle>227</cubicle>"
               "<extension>2145</extension>"
          "</employee>"
     "</accounts_dept>"
     "<engineering_dept>"
          "<employee>"
               "<name>John Smith</name>"
               "<cubicle>372</cubicle>"
               "<extension>2970</extension>"
          "</employee>"
          "<employee level=\"manager\">"
               "<name>Sue Jones</name>"
               "<extension>2375</extension>"
          "</employee>"
     "</engineering_dept>"
"</acme_org>";

Action() {

    int query_number = 1;

// Save data as parameter

    lr.saveString(xml_input, "XML_Input_Param");

    /* Query 1: Find the first employee's name

        (To find the names of all employees

        see Multiple Query Matching)    */

    lr.xmlGetValues("XML={XML_Input_Param}",

        "ValueParam=Result",

        "Query=/acme_org/*/employee/name",

        LAST );

    /* Query 2: Find the first extension number one

        or more levels deep */

    lr.xmlGetValues("XML={XML_Input_Param}",

        "ValueParam=Result",

        "Query=//extension",

        LAST );

    /* Query 3: Find the name of the manager

        of the engineering department. */

    lr.xmlGetValues("XML={XML_Input_Param}",

        "ValueParam=Result",

"Query=/acme_org/engineering_dept/employee[@level=\"manager\"]/name",

        LAST );

    /* Query 4: Find the name of an employee

    whose extension number is 2970 */

    lr.xmlGetValues("XML={XML_Input_Param}",

        "ValueParam=Result",

        "Query=/acme_org/*/employee[extension=\"2970\"]/name",

        LAST );

    return 0;

}

Example: Output from Execution Log Window:
Action.c(33): Saving Parameter "Result = Kevin Sharp"
Action.c(33): "lr.xmlGetValues" was successful, 1 match processed
Action.c(37): Saving Parameter "Result = 2145"
Action.c(37): "lr.xmlGetValues" was successful, 1 match processed
Action.c(41): Saving Parameter "Result = Sue Jones"
Action.c(41): "lr.xmlGetValues" was successful, 1 match processed
Action.c(46): Saving Parameter "Result = John Smith"
Action.c(46): "lr.xmlGetValues" was successful, 1 match processed

Example 2 - Examples of common activities

In this example, there are many variations of common activities. To run it, paste the example into the Action section of a WebServices Vuser script.

Under Deleting elements, there is an example of how to Delete element by a specific attribute value and Delete the first Sign attribute.

Under Replacing and inserting elements, there are samples to show how to Replace all "sign" attributes with an XML fragment, Replace all "sign" attributes with a parameter set, Insert an attribute to the root, and Insert a child in all elements using a parameter.

There is one example of how to Get a fragment.

Under Verify the presence of values in the XML string, you can 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

Under Value related functions, there is an example of how to Get attribute values and Set attribute values.

#include "as_web.h"

/*

Example XML segment is stored in buffer pxml

*/

char* pxml =

     "<abc>"

         "<employee>"

             "<name>Thomas Bertram</name>"

             "<cubicle sign=\"Amateur Theater Club\">"

                "227</cubicle>"

             "<extension>2145</extension>"

             "<manager>"

                 "<name>Lady Maria Bertram</name>"

                 "<extension>2375</extension>"

                 "<extension>0</extension>"

             "</manager>"

         "</employee>"

         "<employee>"

             "<name>Henry Crawford</name>"

             "<cubicle sign=\"Fidelity\">227</cubicle>"

             "<extension>2794</extension>"

             "<manager>"

                 "<name>Admiral Crawford</name>"

                 "<extension>2309</extension>"

                 "<extension>0</extension>"

             "</manager>"

         "</employee>"

         "<employee>"

             "<name>Fanny Price</name>"

             "<cubicle sign=\"Marry my cousin\""

                " test=\"test attrib value\">666</cubicle>"

             "<manager>"

                 "<name>Edmund Bertram</name>"

                 "<extension>2391</extension>"

                 "<extension>0</extension>"

             "</manager>"

         "</employee>"

     "</abc>";

Action()

{

int find_cnt;

// save XML string into parameter

lr.saveString( pxml, "ParamXml" );

    /*

     Deleting elements

    */

    /*Back to top

    Delete element by a specific attribute value

    */

lr.xmlDelete("Xml={ParamXml}",

        "Query=//cubicle[@sign=\"Fidelity\"]",

        "ResultParam=Result",

        LAST

);

//Action.c(59): "lr.xmlDelete" was successful, 1 match processed

/* Delete by specific Sign value:

***********

Input:

    ...

    "<employee>"

         "<name>Henry Crawford</name>"

         "<cubicle sign=\"Fidelity\">227</cubicle>"

         "<extension>2794</extension>"

        ...

     "</employee>"

Output:

    Note that Henry's cubicle has been deleted

...

<employee><name>Henry Crawford</name> <extension>2794</extension>

...

</employee>    *****

*/

    /* Back to top

    Delete the first "Sign" attribute

    */

lr.xmlDelete("Xml={ParamXml}",

        "Query=//cubicle/@sign",

        "ResultParam=Result",

        "SelectAll=yes",

        LAST

);

//Action.c(75): "lr.xmlDelete" was successful, 3 matches processed

/* Delete First Sign :

    ***********

Input:

"<abc>"

         "<employee>"

         "<name>Thomas Bertram</name>"

         "<cubicle sign=\"Amateur Theater Club\">"

            "227</cubicle>"

         "<extension>2145</extension>"

Output:

    Note that Sir Thomas's cubicle no longer has a sign.

<abc> <employee> <name>Thomas Bertram</name> <cubicle>227</cubicle> <extension>2145</extension>

...

    *****

*/

    /* Back to top

    Replacing and inserting elements

    */

    /*

    Replace all "sign" attributes with an XML fragment

    */

lr.xmlReplace( "Xml={ParamXml}",

    "Query=//cubicle/@sign",

    "ResultParam=Result",

    "XmlFragment="

        "NewSign=\"Marry\" NewSign1=\"test\"",

        "SelectAll=yes",

    LAST

);

//Action.c(100): "lr.xmlReplace" was successful, 3 matches processed

lr.message(lr.evalString

    ("/* Replace sign elements:\n***********\n{Result}\n******/"));

/* Replace sign elements:

***********

<abc> <employee> <name>Thomas Bertram</name> <cubicle NewSign="Marry&quot; NewSign1=&quot;test">227</cubicle> <extension>2145</extension> <manager> <name>Lady Maria Bertram</name> <extension>2375</extension> <extension>0</extension> </manager> </employee> <employee> <name>Henry Crawford</name> <cubicle NewSign="Marry&quot; NewSign1=&quot;test">227</cubicle> <extension>2794</extension> <manager> <name>Admiral Crawford</name> <extension>2309</extension> <extension>0</extension> </manager> </employee> <employee> <name>Fanny Price</name> <cubicle NewSign="Marry&quot; NewSign1=&quot;test" test="test attrib value">666</cubicle> <manager> <name>Edmund Bertram</name> <extension>2391</extension> <extension>0</extension> </manager> </employee> </abc>

*****

*/

    /*Back to top

    Replace all sign attributes with a parameter set

    */

lr.saveString

    ( "sign= \"The First New Sign\"", "NewSign_1" );

lr.saveString

    ( "sign= \"The Second New Sign\"", "NewSign_2" );

lr.saveString

    ( "sign= \"The Third New Sign\"", "NewSign_3" );

lr.xmlReplace( "Xml={ParamXml}",

    "Query=//cubicle/@sign",

    "ResultParam=Result",

    "XmlFragmentParam=NewSign",

    "SelectAll=yes",

    LAST

);

lr.message(lr.evalString

    ("/* Replace sign attributes:\n***********\n{Result}\n*****\n*/"));

/* Replace sign attributes:

***********

<abc> <employee> <name>Thomas Bertram</name> <cubicle sign="The First New Sign">227</cubicle> <extension>2145</extension> <manager> <name>Lady Maria Bertram</name> <extension>2375</extension> <extension>0</extension> </manager> </employee> <employee> <name>Henry Crawford</name> <cubicle sign="The Second New Sign">227</cubicle> <extension>2794</extension> <manager> <name>Admiral Crawford</name> <extension>2309</extension> <extension>0</extension> </manager> </employee> <employee> <name>Fanny Price</name> <cubicle test="test attrib value" sign="The Third New Sign">666</cubicle> <manager> <name>Edmund Bertram</name> <extension>2391</extension> <extension>0</extension> </manager> </employee> </abc>

*****

*/

    /*Back to top

     Insert an attribute to the root

"abc" is the root, so the insertion will be outside all the child elements

    */

lr.xmlInsert("Xml={ParamXml}",

    "Query=/abc",

    "ResultParam=Result",

    "Position=attribute",

    "XmlFragment=typeAttr=\"good\"",

    LAST

);

//Action.c(118): "lr.xmlInsert" was successful, 1 match processed

lr.message(lr.evalString

    ("/* Insert root attribute:\n***********\n{Result}\n*****\n*"));

/* Insert root attribute:

***********

<abc> <employee> <name>Thomas Bertram</name> <cubicle sign= ... <employee> <name>Fanny Price</name> ... </employee>typeAttr="good"</abc>

*****

*/

    /* Back to top

    Insert a child in all elements using a parameter

        Only one parameter, ParamComputer_2, is defined.

        For the other undefined elements, "{ParamComputer_n}" is

        inserted.

        */

lr.saveString

    ("<computer>ferrari</computer>",

    "ParamComputer_2");

lr.xmlInsert("Xml={ParamXml}",

    "Query=//employee/manager",

    "ResultParam=Result",

    "XmlFragmentParam=ParamComputer",

    "SelectAll=yes",

    "Position=child",

    LAST

);

//Action.c(140): "lr.xmlInsert" was successful, 3 matches processed

lr.message(lr.evalString

    ("/* Parameter insertion:\n***********\n{Result}\n*****\n*/"));

/* Parameter insertion:

***********

<abc> <employee> <name>Thomas Bertram</name> <cubicle sign="Amateur Theater Club">227</cubicle> <extension> 2145 </extension> <manager> <name>Lady Maria Bertram</name> <extension> 2375 </extension> <extension>0</extension>{ParamComputer_1}</manager> </employee> <employee> <name>Henry Crawford</name> <cubicle sign="Fidelity"> 227</cubicle> <extension>2794</extension> <manager> <name>Admiral Crawford</name> <extension>2309</extension> <extension>0</extension> <computer>ferrari</computer> </manager> </employee> <employee> <name>Fanny Price</name> <cubicle test="test attrib value" sign="Marry my cousin">666</cubicle> <manager> <name>Edmund Bertram</name> <extension>2391</extension> <extension>0</extension>{ParamComputer_3}</manager> </employee> </abc>

*****

*/

    /* Back to top

    Get a fragment

    */

lr.xmlExtract( "Xml={ParamXml}",

    "Query=//cubicle[@sign=\"Fidelity\"]",

    "XMLFragmentParam=Result",

    LAST

);

//Action.c(158): "lr.xmlExtract" was successful, 1 match processed

lr.message(lr.evalString

    ("/* Result:\n***********\n{Result}\n*****\n*"));

/* Result:

    ***********

    <cubicle sign="Fidelity">227</cubicle>

    *****

    */    

    /*Back to top

     Verify the presence of values in the XML string

    */

    /*

    Finding the first occurrence

    This is the first occurrence, so the default

    "SelectAll=no does not prevent the find from working */

find_cnt = lr.xmlFind( "Xml={ParamXml}",

    "Query=//cubicle/@sign",

    "Value=Amateur Theater Club",

    LAST

);

//Action.c(177): "lr.xmlFind" was successful, 1 match processed

lr.message

    ("sign = Amateur Theater Club: Found %d matches", find_cnt);

    /*

    sign = Amateur Theater Club: Found 1 matches

    */

    /* Back to top

    Try to find a partial string

    The complete string is "Marry my cousin".

    lr.xmlFind will not find the partial string "Marry"

    as a literal value */

find_cnt = lr.xmlFind( "Xml={ParamXml}",

    "Query=//cubicle/@sign",

    "Value=Marry",

    "SelectAll=yes",

    "NotFound=Continue",

    LAST

);

/*

Action.c(189): Error: no matches were found for the specified query [class:CLrXmlScriptFunc]

Action.c(189): Error: "lr.xmlFind" execution failed

Action.c(189): Continuing after error in Vuser script.

*/

    /* Back to top

     Finding a partial string with regular expressions

        To find a partial string, you can use  regular expressions. Here we find "Marry" at the beginning of the value */

find_cnt = lr.xmlFind( "Xml={ParamXml}",

    "Query=//cubicle/@sign",

    "Value=^Marry",

    "SelectAll=yes",

    "UseRegExp=yes",

    LAST

);

//Action.c(204): "lr.xmlFind" was successful, 1 match processed

lr.message

    ("sign = ^Marry: Found %d matches", find_cnt);

    //sign = ^Marry: Found 1 matches

    /* Here we find "my cousin" at the end of the

        value */

find_cnt = lr.xmlFind( "Xml={ParamXml}",

    "Query=//cubicle/@sign",

    "Value=my cousin$",

    "SelectAll=yes",

    "UseRegExp=yes",

    LAST

);

// Action.c(215): "lr.xmlFind" was successful, 1 match processed

lr.message("sign = my cousin$: Found %d matches", find_cnt);

    //sign = my cousin$: Found 1 matches

    /* Here we find "my" anywhere except the beginning or

        end of the value */

find_cnt = lr.xmlFind( "Xml={ParamXml}",

    "Query=//cubicle/@sign",

    "Value=.+my.+",

    "SelectAll=yes",

    "UseRegExp=yes",

    LAST

);

lr.message("sign = .+my.+: Found %d matches", find_cnt);

//sign = .+my.+: Found 1 matches

    /*Back to top

     Finding a complete string that is not the first occurrence

        lr.xmlFind will find the entire string, but unless

        SelectAll=yes is specified, it will only find the

        first occurrence */

find_cnt = lr.xmlFind( "Xml={ParamXml}",

    "Query=//cubicle/@sign",

    "Value=Marry my cousin",

    "NotFound=Continue",

    LAST

);

/*

Action.c(237): Error: no matches were found for the specified query [class:CLrXmlScriptFunc]

Action.c(237): Error: "lr.xmlFind" execution failed

Action.c(237): Continuing after error in Vuser script.

*/

lr.message

("sign = Marry my cousin (SelectAll default = no): Found %d matches", find_cnt);

    /*

    sign = Marry my cousin (SelectAll default = no): Found 0 matches

    */

    /* Try the same query with SelectAll=yes */

find_cnt = lr.xmlFind( "Xml={ParamXml}",

    "Query=//cubicle/@sign",

    "Value=Marry my cousin",

    "SelectAll=yes",

    LAST

);

//Action.c(248): "lr.xmlFind" was successful, 1 match processed

lr.message

("sign = Marry my cousin (SelectAll=Yes): Found %d matches", find_cnt);

/*

sign = Marry my cousin (SelectAll=Yes): Found 1 matches

*/

    /* Back to top

    Specifying the occurrence

        Without specifying SelectAll=yes, lr.xmlFind will

        still find a specified employee occurrence, [3].

        Note that "Query=//cubicle[3]/@sign" will not work.

        "cubicle[3]" would look for the third cubicle under

        any employee.

     */

find_cnt = lr.xmlFind( "Xml={ParamXml}",

    "Query=//employee[3]/cubicle/@sign",

    "Value=Marry my cousin",

    LAST

);

// Action.c(287): "lr.xmlFind" was successful, 1 match processed

lr.message

    ("Third employee, sign = Marry my cousin (SelectAll default = no): Found %d matches", find_cnt);

    /*

    Third employee, sign = Marry my cousin (SelectAll default = no): Found 1 matches

    */

    /*Back to top

     Value related functions

        Value functions make no changes in the

        XML structure, but change values

    */

    /*

    Get attribute values

    This example returns the first occurrence */

lr.xmlGetValues( "Xml={ParamXml}",

    "Query=//cubicle/@sign",

    "ValueParam=ResultValue",

    LAST

);

//Action.c(265): "lr.xmlGetValues" was successful, 1 match processed

lr.message (lr.evalString

("*/\n\n/* Get sign values:\n***********\n{ResultValue}\n*****\n*/\n/*"));

    /* Get sign values:

    ***********

    Amateur Theater Club

    *****

    */

    /*Back to top

     Set attribute values

    */

    

/*

Example of common error:

This is common mistake when trying replace the value of an attribute.

Note that if you ran this query:

lr.xmlSetValues( "Xml={ParamXml}",

    "Query=//cubicle[@sign=\"Marry my cousin\"]",

    "Value=Do not marry Henry",

    "ResultParam=Result",

    LAST

);

It would find the element whose sign is "Marry my cousin",

which is

<employee><name>Fanny Price</name>

    <cubicle sign="Marry my cousin"

        test="test attrib value">666

    </cubicle>

and replace the value, "666", with the phrase

"Do not marry Henry"

This would be the result:

<employee><name>Fanny Price</name>

    <cubicle test="test attrib value"

        sign="Marry my cousin">Do not marry Henry

    </cubicle>

The intent is to replace the value of the sign attribute.

Use the format in the query below:

"Query=//cubicle[@sign=\"Marry my cousin\"]/@sign"

This means, find the sign attribute of the element whose

sign attribute value is "Marry my cousin".

The following command replaces the value of the attribute, and not of the element.

*/

lr.xmlSetValues("Xml={ParamXml}",

    "Query=//cubicle[@sign=\"Marry my cousin\"]/@sign",

    "Value=Do not marry Henry",

    "ResultParam=Result",

    LAST

);

//Action.c(313): "lr.xmlSetValues" was successful, 1 match processed

lr.message(lr.evalString

("*/\n\n/* Do not marry Henry:\n***********\n{Result}\n*****\n*/\n/*"));

/* Do not marry Henry:

***********

<abc> <employee> <name>Thomas Bertram</name> <cubicle sign="Amateur Theater Club">227</cubicle> <extension>2145</extension> <manager> <name>Lady Maria Bertram</name> <extension>2375</extension> <extension>0</extension> </manager> </employee> <employee> <name>Henry Crawford</name> <cubicle sign="Fidelity">227</cubicle> <extension>2794</extension> <manager> <name>Admiral Crawford</name> <extension>2309</extension> <extension>0</extension> </manager> </employee> <employee> <name>Fanny Price</name> <cubicle test="test attrib value" sign="Do not marry Henry">666</cubicle> <manager> <name>Edmund Bertram</name> <extension>2391</extension> <extension>0</extension> </manager> </employee> </abc>

*****

*/

    return 0;

}

Back to top