問題描述
WSO2 豐富陣列 (WSO2 Enrich array)
我正在使用 WSO2 ESB (4.8.1),我需要轉換此有效負載:
[
{
"id":"1",
"budget":"a"
},
{
"id":"2",
"bidget:"b"
}
]
在此使用豐富的中介(如果可能的話):
[
{
"id":"1",
"budget":"a",
"result":"1‑a"
},
{
"id":"2",
"bidget:"b",
"result":"2‑b"
}
]
任何建議?
提前致謝
參考解法
方法 1:
Take a look a this sample:
Input file:
<?xml version="1.0" encoding="UTF‑8" ?>
<employees>
<root>
<id>1</id>
<budget>a</budget>
</root>
<root>
<id>2</id>
<budget>b</budget>
</root>
<root>
<id>3</id>
<budget>c</budget>
</root>
</employees>
My xslt:
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<employees>
<xsl:for‑each select="employees/root">
<root>
<id>
<xsl:value‑of select="id"/>
</id>
<budget>
<xsl:value‑of select="budget"/>
</budget>
<result>
<xsl:value‑of select="concat(id,'‑',budget)"/>
</result>
</root>
</xsl:for‑each>
</employees>
</xsl:template>
</xsl:stylesheet>
Output file:
<?xml version="1.0" encoding="UTF‑8" ?>
<employees>
<root>
<id>1</id>
<budget>a</budget>
<result>1‑a</result>
</root>
<root>
<id>2</id>
<budget>b</budget>
<result>2‑b</result>
</root>
<root>
<id>3</id>
<budget>c</budget>
<result>3‑c</result>
</root>
</employees>
So you can use an xslt mediator in WSO2 ESB(https://docs.wso2.com/display/ESB490/XSLT+Mediator ) and this configuration or similar and work with your definitions. Regards.
方法 2:
I think when you don't know about the length of array you con't transform it with enrich or payloadFactory. I found same problem in WSO2 ESB document. this JS function transform an array of objects.
function transform(mc) {
payload = mc.getPayloadJSON();
results = payload.results;
var response = new Array();
for (i = 0; i < results.length; ++i) {
location_object = results[i];
l = new Object();
l.name = location_object.name;
l.tags = location_object.types;
l.id = "ID:" + (location_object.id);
response[i] = l;
}
mc.setPayloadJSON(response);
}
See scipte mediator in this link for more information.
(by Joaquin、Jorge Infante Osorio、Milad Kianmehr)