任务:将如下输入XML变成如下输出XML
输入XML:
<employees><employee><id>001</id><name>张飞</name><age>28</age></employee><employee><id>002</id><name>关羽</name><age>29</age></employee><employee> <id>003</id><name>赵云</name><age>30</age></employee></employees>
输出XML:
<summary><count>3</count><names>张飞,关羽,赵云,</names><maxAge>30</maxAge><avgAge>2.9E+1</avgAge></summary>
我们需要做一个Input节点和一个Output节点以及一个Comput节点,并设置Compute节点的ESQL如下:
CREATE COMPUTE MODULE flow_Compute
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
-- 员工总数
DECLARE Count INTEGER 0;
-- 得到XML中员工总数
SET Count=CARDINALITY(InputRoot.XML.employees.employee[]);
-- 员工姓名汇总
DECLARE Names CHARACTER;
SET Names='';
-- 最大年龄
DECLARE MaxAge INTEGER 0;
-- 年龄合计
DECLARE SumAge INTEGER 0;
-- 当前年龄
DECLARE CurrAge INTEGER 0;
-- 循环变量
DECLARE I INTEGER 1;
WHILE I<=Count DO
-- 字符串拼接
SET Names=Names || InputRoot.XML.employees.employee[I].name ;
SET Names=Names || ',';
-- 字符串转换成整数再进行年龄合计
SET CurrAge = CAST(InputRoot.XML.employees.employee[I].age AS INTEGER);
SET SumAge=SumAge+CurrAge;
-- 得到最大年龄
IF MaxAge<CurrAge THEN
SET MaxAge=CurrAge;
END IF;
SET I=I+1;
END WHILE;
-- 平均年龄
DECLARE AvgAge FLOAT;
SET AvgAge=SumAge/Count;
-- 拼接输出XML
SET OutputRoot.XML.summary.count=Count;
SET OutputRoot.XML.summary.names=Names;
SET OutputRoot.XML.summary.maxAge=MaxAge;
SET OutputRoot.XML.summary.avgAge=CAST(AvgAge AS CHARACTER);
RETURN TRUE;
END;
END MODULE;
本例工程下载地址:
http://www.blogjava.net/Files/heyang/ESqlTest0920_01.rar