Share this page

# JavaScript XML query examples

The scripts below can be run to illustrate XML queries. To run the examples, paste the segments into the Actions section of your script. Make sure to enable the Extended Log in the VuGen Runtime Settings with Parameter substitution, so that you can view the values of the result parameter in the Execution log. For details, see the VuGen Online Help Center.

In this topic:

## Example: Get values

The lr.xmlGetValues function 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: common actions

In this example, there are several variations of common actions. To run them, paste the example into the Action section of a WebServices Vuser script.

Example
Store XML segment in buffer pxml
Delete element by a specific attribute value
Delete the first "Sign" attribute
Replace all "sign" attributes with an XML fragment
Replace all sign attributes with a parameter set
Insert an attribute to the root
Insert a child in all elements using a parameter
Get a fragment
Verify the presence of values in the XML string
Find the first occurrence
Find a partial string
Find a partial string with regular expressions
Find a complete string that is not the first occurrence
Specify the occurrence
Value related functions
Get attribute values
Set attribute values

### Store XML segment in buffer pxml

#include "as_web.h"

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" );

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

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

...

*****

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

*****

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

*****

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

*****

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

*/

/*

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

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

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

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

### Specify 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 it does 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

*****

*/

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

}

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