lr_xml_find()
驗證查詢返回的XML值。
int lr_xml_find(<規範列表> [,<可選規範列表]],[LAST]);
參數說明:
List of specifications:對於以下所需規格的列表,請使用以下字符串格式:
“Specification = value”
Query:對輸入字符串XML的XML查詢或快速查詢。
XML:要查詢的XML輸入字符串
選擇以下選項之一:
Value:要查找的字符串。這可以是元素值或其屬性值。
or:
ValueParam:包含要查找的字符串的參數名稱。
List of optional specifications:對於以下可選規格列表,請使用以下字符串格式:
“Specification = value”
SelectAll:如果爲“yes”,則將處理與查詢匹配的所有元素。如果“否”,則僅驗證第一個查詢匹配。默認值爲“no”。請參閱多查詢匹配
IgnoreCase:如果爲“yes”,則搜索將忽略Value或ValueParam的大寫和小寫字符與查詢結果之間的差異。默認值爲“no”。
UseRegExp:如果爲“yes”,Value和ValueParam可以是函數將搜索的正則表達式。有關詳細信息,請參閱正則表達式。默認值爲“UseRegExp = no”。
NotFound:指定在找不到搜索值時腳本是失敗還是繼續。值爲“繼續”或“錯誤”。如果未指定NotFound,則腳本將失敗。
LAST:表示可選規範列表結束的標記
lr_xml_find函數查詢XML輸入字符串XML中與查詢條件(Value或ValueParam)匹配的值,並返回出現次數。如果SelectAll爲“no”,lr_xml_find返回1或0。
示例:lr_xml_find
以下示例將“擴展”標記插入到員工記錄中。它首先使用lr_xml_find驗證僱員John Smith的記錄是否存在於XML字符串xml_input中。如果它不存在,那麼lr_xml_find將終止腳本的執行併發送錯誤消息。
但是,如果在運行時設置中選擇繼續運行錯誤或“NotFound = continue”傳遞到lr_xml_find,腳本將繼續。在這些情況下,檢查匹配數將阻止lr_xml_insert執行。有關處理錯誤條件的更多選項,請參閱lr_exit。
如果find操作成功,lr_xml_insert在xml_input中搜索標籤“<employee>”,並插入包含擴展信息的XML片段。 (“XmlFragment = <extension> 2145 </ extension>”)。
結果字符串包含在ResultParam參數Result中。
有關使用lr_xml_find的更多示例,請參見“查找第一個出現”,“嘗試查找部分字符串”,“使用正則表達式查找部分字符串”,“查找不是第一個出現的完整字符串”以及在“XML查詢的示例腳本”的“XML字符串”部分中的“驗證值是否存在”中的“指定發生”。
#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”);
/ *驗證員工John Smith是否存在* /
find_cnt = lr_xml_find(“XML = {XML_Input_Param}”,
“Value = John Smith”,
“Query = / acme_org / employee”,
LAST);
if(find_cnt> 0)
{
/ *現在插入John Smith的電話分機號碼* /
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(“Insert after String:{Result}”)));
} // end if find_cnt> 0
return 0;
}}
輸出:
Action.c(15):“lr_xml_find”成功,處理1個匹配
Action.c(20):“lr_xml_insert”成功,處理1個匹配
Action.c(25):插入後的字符串:<acme_org> <employee> John Smith <cubicle> 227 </ cubicle> <extension> 2145 </ extension> </ employee> </ acme_org>
實施例2
以下示例按級別屬性的值搜索。在第一個實例中,找到文本管理器。在第二個例子中,沒有找到導演的匹配。
lr_xml_find(“Xml = {XML_Input_Param}”,
“Query = / acme_org / employee / @ level”,
“Value = manager”,
LAST
);
lr_xml_find(“Xml = {XML_Input_Param}”,
“Query = / acme_org / employee / @ level”,
“Value = director”,
LAST
);
輸出(在執行日誌或output.txt文件中):
Action.c(14):“lr_xml_find”成功,處理1個匹配
Action.c(19):錯誤:找不到指定的查詢的匹配[class:CLrXmlScriptFunc]
Action.c(19):錯誤:“lr_xml_find”執行失敗