ran*_*lem 5 javascript iterator wso2 wso2-esb wso2-micro-integrator
我使用 Iterator 和 Script Mediator 来计算学生成绩的总和。
我看到这个问题,我每次收到0和note的总和,你说totalnote不接受变量note和增量例如,如果我把totalnote的值= 16,它是16和每行的note的总和
我的目标是将这 4 个音符加起来
这是我的代码
<property name="totalnote" scope="default" type="INTEGER" value="0"/>
<iterate expression="//etudiants/etudiant">
<target>
<sequence>
<property expression="json-eval($.etudiant.note)" name="note" scope="default" type="INTEGER"/>
<log level="custom">
<property expression="get-property('note')" name="msg"/>
</log>
<script language="js"><![CDATA[{var i = parseInt(mc.getProperty("note")) ;
var totalnote = parseInt(mc.getProperty("totalnote")) ;
var totalnote = totalnote + i;
totalnote=totalnote.toString();
mc.setProperty("totalnote", totalnote);}]]></script>
<log level="custom">
<property expression="get-property('totalnote')" name="ms"/>
</log>
</sequence>
</target>
</iterate>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Run Code Online (Sandbox Code Playgroud)
我收到的结果:
[2022-12-01 10:04:40,450] INFO {LogMediator} - {api:student} msg = 13
[2022-12-01 10:04:40,450] INFO {LogMediator} - {api:student} msg = 15
[2022-12-01 10:04:40,450] INFO {LogMediator} - {api:student} msg = 16
[2022-12-01 10:04:40,450] INFO {LogMediator} - {api:student} msg = 17
[2022-12-01 10:04:40,469] INFO {LogMediator} - {api:student} ms = 17
[2022-12-01 10:04:40,469] INFO {LogMediator} - {api:student} ms = 13
[2022-12-01 10:04:40,469] INFO {LogMediator} - {api:student} ms = 15
[2022-12-01 10:04:40,469] INFO {LogMediator} - {api:student} ms = 16
Run Code Online (Sandbox Code Playgroud)
note如果您只需要获得所有学生的总和,您可以简单地使用以下 XPath 表达式,
<property expression="sum(//etudiants/etudiant/note[number(.) = number(.)])" name="totalnote" scope="default" type="STRING"/>
Run Code Online (Sandbox Code Playgroud)
根据您的有效负载,您可能需要更新表达式。这里我假设了如下所示的有效负载,
{
"etudiants": {
"etudiant": [
{
"note": 17
},
{
"note": 13
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
iterate mediator如果您想在每次迭代中进行后端调用,则应该使用。在这里,对于每次迭代,都会创建一条新消息,您需要通过Aggregate mediator.
如果您只想迭代有效负载,您可以使用ForEach Mediator. ForEach Mediator 也支持修改原始有效负载。
欲了解更多信息:https://apim.docs.wso2.com/en/latest/reference/mediators/iterate-mediator/