XML Functions with Loadrunner

上一篇 / 下一篇  2011-02-28 15:03:41 / 个人分类:LoadRunner

XML Function

Retrieves values of XML elements found by a query
Sets the values of XML elements found by a query
Extracts XML string fragments from an XML string
Deletes fragments from an XML string
Replaces fragments of an XML string
Inserts a new XML fragment into an XML string
Verifies that XML values are returned by a query
Applies Extensible Stylesheet Language (XSL) Transformation to XML data

如果要找到XML元素指定属性名,使用@attribute_name
[@attribute_name=\"value\"].
实例中节点:<employee level="manager">
XMLQuery = /employee[@level=\"manager\"]


lr_xml_get_values
XML:the XML Input String to query
ValueParam:the name of the output parameter which stores the result of the query,if not exist,it's created.
Query:the XML Query or Fast Query on the input string XML.
SelectAll:yes-Multiple Query Matching
所有的XML函数都是返回找到的匹配数或者是0;

例一(XML Query):
#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;
}


例二(Multiple Query Matching)
在默认情况下,所有的函数都是返回第一个XML Query匹配的项。Multiple Query Matching可以
处理每一个连续的匹配项,并且把每个匹配项写入到参数集合中(Parameter Set)
Param_1, Param_2, Param_3, ...
#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;
}

 

 

lr_xml_set_values

设置搜索到的XML元素的值。
XML:the XML Input String to query
ResultParam:the output parameter containing the XML data after setting the new value.(包含新值的输出参数)
Query:the XML Query on the input string XML.
Value:the string value to set as the XML element
ValueParam:the parameter name containing the value to set as the XML element.(包含设置为XML元素值的参数名)
SelectAll:yes-Multiple Query Matching

 

例三(XML Set):
#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
    lr_save_string("1111", "ExtensionParam_1");
    lr_save_string("2222", "ExtensionParam_2");
    lr_xml_set_values("XML={XML_Input_Param}",
       "ResultParam=NewXmlParam",
       "ValueParam=ExtensionParam",
       "SelectAll=yes",
       "Query=//extension",
       LAST);
 NumOfValues=lr_xml_get_values("XML={NewXmlParam}",
          "ValueParam=OutputParam",
          "Query=//extension",
          "SelectAll=yes",
          LAST);
 for(i=0;i<NumOfValues;i++) {/* Print the multiple values of MultiParam */

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

 return 0;
}

 

lr_xml_extract
Extracts XML fragments from an XML string.
XML:the XML Input String to query.
XMLFragmentParam:the name of the output parameter containing the extracted XML string fragment.(包含抽取的XML字符片段的输出参数)
Query: the XML Query or Fast Query on the input string XML.
SelectAll: If "yes", Multiple Query Matching
ResolveNameSpaces:If "no", then XMLFragmentParam will contain the extracted string as it appears in the XML input. If "yes", then XMLFragmentParam will include full resolution of any namespace prefixes (e.g., "a:body") in the XML input string. The Default is "no".

例四(XML Extract):
#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 level=\"manager\">"
               "<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
     lr_xml_extract("XML={XML_Input_Param}",
                    "XMLFragmentParam=Result",
     "Query=/acme_org/engineering_dept/employee",
     LAST);
     lr_output_message(lr_eval_string("Extracted: {Result}"));

     return 0;
}

lr_xml_delete
The lr_xml_delete function queries the XML input string XML and deletes the fragments of the XML
tree that match the Query criteria.you can delete elements by specifying the element name or its
attribute in the XML Query.The output parameter ResultParam contains the modified XML string subsequent
to deletion.
XML: the XML Input String to query
ResultParam: the name of the output parameter containing the XML data after deleting the fragment.
Query: the XML Query on the input string XML.
SelectAll: If "yes",Multiple Query Matching

Note: After deleting the contents of an element, the empty element, e, is signified <e/>. For example, the string result after deleting the element c from the XML string "<a><b><c></c></b></a>" is:
"<a><b/></a>"
since the element b is now empty.

例五(XML Delete):
#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>"
"</acme_org>";
Action() {

    lr_save_string(xml_input, "XML_Input_Param"); // Save input as parameter
    lr_xml_delete("XML={XML_Input_Param}",
        "ResultParam=Result",
        "Query=/acme_org/accounts_dept/employee/extension",
  LAST);
    lr_output_message(lr_eval_string("String after deletion: {Result}"));
    return 0;

}


lr_xml_replace
Replaces fragments of an XML string.
XML: the XML Input String to query
ResultParam: the name of the output parameter containing the XML data after replacing the new value
Query: the XML Query on the input string XML.
XmlFragment: the string value to use as replacement of the query match—an element or an attribute.
XmlFragmentParam: the name of the parameter containing the string value to use as replacement
SelectAll: If "yes",Multiple Query Matching


The lr_xml_replace function queries the XML input string XML for values matching the Query criteria
and replaces them either with XMLFragment or XMLFragmentsParam as the value of the elements matched by
the query, you can replace elements by specifying either its element name or attribute in the XML Query

If there is more than one value to be replaced, then pass the "XmlFragmentParam=" specification. Save the values in a series of parameters with the names:
Param_1, Param_2, Param_3, ...

例六(XML Replace):
#include "as_web.h"
char *xml_input =
"<acme_org>"
     "<employee>"
          " <name>John Smith</name>"
          "<cubicle>227</cubicle>"
          "<extension>2145</extension>"
     "</employee>"
"</acme_org>";
Action() {

     lr_save_string(xml_input, "XML_Input_Param");
  lr_xml_replace("XML={XML_Input_Param}",
     "ResultParam=Result",
     "Query=/acme_org/employee/extension",
     "XmlFragment=<extension>4444</extension>",
     LAST);
  lr_output_message(lr_eval_string("String after replacement:{Result}"));
  return 0;
}


lr_xml_insert
Inserts a new XML fragment into an XML string
The lr_xml_insert function queries the xml input string XML for values matching the Query criteria.
It then insets XmlFragment or XmlFragmentParam at the position in the XML string returned by the Query.

XML: the XML Input String to query
ResultParam: The output parameter containing the XML data after inserting the new fragment.
Query: the XML Query on the input string XML.
XmlFragment: the string to insert. It can be a new element or a attribute of an existing element.
XmlFragmentParam: the name of the parameter containing the string value to insert
SelectAll: If "yes",Multiple Query Matching
Position: the position to insert the XML fragment. Choose one of:
- before: place the fragment before the tag returned
               by a Query
- after: place the fragment after the tag returned
               by a Query (this is the default value)
- child: place the fragment as a child of the tag
           returned by a Query
- attribute: indicates an attribute of an element
               returned by a Query

Postion specifies whether the insertion is done before or after the point returned.Additionally, the child Position specifies that the fragment is inserted before the end of the tag found by the query. For example, if the input string is
<a>53</a>
an inserted fragment ("<b>ZZ</b>") in the child position will result in the string:
<a>53<b>ZZ</b></a>

例七(XML Insert):
#include "as_web.h"
char *xml_input =
"<acme_org>"
     "<employee>"
          " <name>John Smith</name>"
          "<cubicle>227</cubicle>"
     "</employee>"
"</acme_org>";
Action() {
 lr_save_string(xml_input, "XML_Input_Param");
 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}"));

     return 0;
}

 

 

lr_xml_find
Verifies that XML values are returned by a query
Query: the XML Query or Fast Query on the input string XML.
XML: the XML Input String to query
Value: the string to find. This can be the element value or its attribute value.
ValueParam: the parameter name containing the string to find.
SelectAll: If "yes",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
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.

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.
例八(XML Find):
#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;
}


lr_xml_transform
The lr_xml_transform. function transforms the XML input string XML using the Extensible Stylesheet Language (XSL) specifications in Stylesheet and saves the resulting formatted data in ResultParam,
XML: the XML Input String to query
Stylesheet: the XSL specifications. Has the format of an XSL string.
ResultParam: the name of the output parameter containing the XML formatted data after XSL transformation

例九(XML Transfer):
#include "as_web.h"
char *xml_input =
"<?xml version=\"1.0\" ?>"
     "<sales>"
          "<summary>"
               "<heading>Acme Organization</heading>"
               "<subhead>IT Management Report</subhead>"
               "<description>Report by Department</description>"
          "</summary>"
     "</sales>";

char *stylesheet =
"<?xml version=\"1.0\"?>"
"<xsl:stylesheet xmlns:xsl=\"
http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"
"<xsl:output method=\"html\"/>"
"<xsl:template match=\"/\">"
     "<HTML>"
          "<HEAD>"
               "<TITLE><xsl:value-of select=\"//summary/heading\"/></TITLE>"
          "</HEAD>"
          "<BODY>"
               "<h1><xsl:value-of select=\"//summary/heading\"/></h1>"
               "<h2><xsl:value-of select=\"//summary/subhead\"/></h2>"
               "<p><xsl:value-of select=\"//summary/description\"/></p>"
          "</BODY>"
     "</HTML>"
"</xsl:template>"
"</xsl:stylesheet>";

Action() {

     lr_save_string(xml_input, "XML_Input_Param"); // Save to a parameter
     lr_save_string(stylesheet, "XML_StyleSheet_Param");// Save to a parameter
     lr_xml_transform("XML={XML_Input_Param}", "ResultParam=Result",
       "Stylesheet={XML_StyleSheet_Param}",
       LAST);
     lr_output_message(lr_eval_string("String after transformation: {Result}"));
     return 0;
}

 


TAG:

 

评分:0

我来说两句

Open Toolbar