ESB WSO2 使用 Enrich、Iterate 和 Filter mediator 添加 属性 到 XML 文件
ESB WSO2 Adding a property into a XML file using Enrich, Iterate and Filter mediator
我想向我的 api:
发送带有 xml 文件的 Postman 请求
<data>
<user>
<name>Andrei</name>
<adress>Tudor Vladimirescu, nr2</adress>
<cnp>123456789</cnp>
<age>22</age>
</user>
<user>
<name>Ana</name>
<adress>Str. Pacurari, nr1</adress>
<cnp>123456789</cnp>
<age>26</age>
</user>
<user>
<name>Andreea</name>
<adress>Tudor Vladimirescu, nr1</adress>
<cnp>123456789</cnp>
<age>20</age>
</user>
</data>
我想根据年龄为每个 <user>
添加一个 <valid>Yes/No</valid>
标签。例如:如果年龄在 23 岁以上,那么 valid
标签的值应该是 Yes
。我想将修改后的消息发送给另一个 API。
我知道我应该使用 Enrich、Iterate 和 Filter 中介,但我真的需要一些帮助,因为我是初学者。谢谢!
我使用了 Iterate
调解器,因为你需要后端调用,filter
调解器用于设置每个元素的年龄是否有效,最后使用 enrich
调解器来实现你的用例.
<!-- iteration of incoming user data -->
<iterate expression="//data/user" id="iterateuser" sequential="true">
<target>
<sequence>
<property expression="//age/text()" name="age" scope="default" type="STRING"/>
<log level="custom">
<property name="Inside Iterator" value="is called*****"/>
<property expression="get-property('age')" name="age"/>
</log>
<!-- fiter to find valid age or not-->
<filter xpath="get-property('age')>='23'">
<then>
<property name="valid" scope="default" type="STRING" value="Yes"/>
</then>
<else>
<property name="valid" scope="default" type="STRING" value="No"/>
</else>
</filter>
<!-- enrich to add valid tag to existing payload-->
<enrich>
<source clone="true" type="inline">
<valid xmlns=""/>
</source>
<target action="child" xmlns:ns="http://www.wso2.org/types" xpath="//user"/>
</enrich>
<!-- enrich to add valid property value with validtag-->
<enrich>
<source clone="true" property="valid" type="property"/>
<target xpath="//valid"/>
</enrich>
<log level="full"/>
<!-- make an external endpoint call here -->
</sequence>
</target>
</iterate>
输入:
<data>
<user>
<name>Andrei</name>
<adress>Tudor Vladimirescu, nr2</adress>
<cnp>123456789</cnp>
<age>22</age>
</user>
<user>
<name>Ana</name>
<adress>Str. Pacurari, nr1</adress>
<cnp>123456789</cnp>
<age>26</age>
</user>
<user>
<name>Andreea</name>
<adress>Tudor Vladimirescu, nr1</adress>
<cnp>123456789</cnp>
<age>20</age>
</user>
</data>
迭代日志:
[2022-03-15 12:27:23,032] INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 22
[2022-03-15 12:27:23,032] INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:8101fdcc-0a29-4bea-a8f1-b8ad80f0c6ff, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
<name>Andrei</name>
<adress>Tudor Vladimirescu, nr2</adress>
<cnp>123456789</cnp>
<age>22</age>
<valid>No</valid></user></soapenv:Body></soapenv:Envelope>
[2022-03-15 12:27:23,032] INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 26
[2022-03-15 12:27:23,040] INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:b01f8b29-9fe5-43e6-a0ed-0a61d395fd1a, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
<name>Ana</name>
<adress>Str. Pacurari, nr1</adress>
<cnp>123456789</cnp>
<age>26</age>
<valid>Yes</valid></user></soapenv:Body></soapenv:Envelope>
[2022-03-15 12:27:23,053] INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 20
[2022-03-15 12:27:23,053] INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:1feeb723-8a72-475a-b014-cda2e7b7a90a, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
<name>Andreea</name>
<adress>Tudor Vladimirescu, nr1</adress>
<cnp>123456789</cnp>
<age>20</age>
<valid>No</valid></user></soapenv:Body></soapenv:Envelope>
如果您注意到上面的日志 <valid>No</valid>
或 <valid>Yes</valid>
已添加到负载中。
Post这个,你可以在iterator内部进行外部端点调用。
参考:
Enrich Mediator
我想向我的 api:
发送带有 xml 文件的 Postman 请求<data>
<user>
<name>Andrei</name>
<adress>Tudor Vladimirescu, nr2</adress>
<cnp>123456789</cnp>
<age>22</age>
</user>
<user>
<name>Ana</name>
<adress>Str. Pacurari, nr1</adress>
<cnp>123456789</cnp>
<age>26</age>
</user>
<user>
<name>Andreea</name>
<adress>Tudor Vladimirescu, nr1</adress>
<cnp>123456789</cnp>
<age>20</age>
</user>
</data>
我想根据年龄为每个 <user>
添加一个 <valid>Yes/No</valid>
标签。例如:如果年龄在 23 岁以上,那么 valid
标签的值应该是 Yes
。我想将修改后的消息发送给另一个 API。
我知道我应该使用 Enrich、Iterate 和 Filter 中介,但我真的需要一些帮助,因为我是初学者。谢谢!
我使用了 Iterate
调解器,因为你需要后端调用,filter
调解器用于设置每个元素的年龄是否有效,最后使用 enrich
调解器来实现你的用例.
<!-- iteration of incoming user data -->
<iterate expression="//data/user" id="iterateuser" sequential="true">
<target>
<sequence>
<property expression="//age/text()" name="age" scope="default" type="STRING"/>
<log level="custom">
<property name="Inside Iterator" value="is called*****"/>
<property expression="get-property('age')" name="age"/>
</log>
<!-- fiter to find valid age or not-->
<filter xpath="get-property('age')>='23'">
<then>
<property name="valid" scope="default" type="STRING" value="Yes"/>
</then>
<else>
<property name="valid" scope="default" type="STRING" value="No"/>
</else>
</filter>
<!-- enrich to add valid tag to existing payload-->
<enrich>
<source clone="true" type="inline">
<valid xmlns=""/>
</source>
<target action="child" xmlns:ns="http://www.wso2.org/types" xpath="//user"/>
</enrich>
<!-- enrich to add valid property value with validtag-->
<enrich>
<source clone="true" property="valid" type="property"/>
<target xpath="//valid"/>
</enrich>
<log level="full"/>
<!-- make an external endpoint call here -->
</sequence>
</target>
</iterate>
输入:
<data>
<user>
<name>Andrei</name>
<adress>Tudor Vladimirescu, nr2</adress>
<cnp>123456789</cnp>
<age>22</age>
</user>
<user>
<name>Ana</name>
<adress>Str. Pacurari, nr1</adress>
<cnp>123456789</cnp>
<age>26</age>
</user>
<user>
<name>Andreea</name>
<adress>Tudor Vladimirescu, nr1</adress>
<cnp>123456789</cnp>
<age>20</age>
</user>
</data>
迭代日志:
[2022-03-15 12:27:23,032] INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 22
[2022-03-15 12:27:23,032] INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:8101fdcc-0a29-4bea-a8f1-b8ad80f0c6ff, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
<name>Andrei</name>
<adress>Tudor Vladimirescu, nr2</adress>
<cnp>123456789</cnp>
<age>22</age>
<valid>No</valid></user></soapenv:Body></soapenv:Envelope>
[2022-03-15 12:27:23,032] INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 26
[2022-03-15 12:27:23,040] INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:b01f8b29-9fe5-43e6-a0ed-0a61d395fd1a, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
<name>Ana</name>
<adress>Str. Pacurari, nr1</adress>
<cnp>123456789</cnp>
<age>26</age>
<valid>Yes</valid></user></soapenv:Body></soapenv:Envelope>
[2022-03-15 12:27:23,053] INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 20
[2022-03-15 12:27:23,053] INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:1feeb723-8a72-475a-b014-cda2e7b7a90a, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
<name>Andreea</name>
<adress>Tudor Vladimirescu, nr1</adress>
<cnp>123456789</cnp>
<age>20</age>
<valid>No</valid></user></soapenv:Body></soapenv:Envelope>
如果您注意到上面的日志 <valid>No</valid>
或 <valid>Yes</valid>
已添加到负载中。
Post这个,你可以在iterator内部进行外部端点调用。
参考: Enrich Mediator