WSO2 豐富陣列 (WSO2 Enrich array)


問題描述

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 JoaquinJorge Infante OsorioMilad Kianmehr)

參考文件

  1. WSO2 Enrich array (CC BY‑SA 2.5/3.0/4.0)

#wso2-esb #wso2






相關問題

WSO2 Carbon 管理控制台異常 (WSO2 Carbon Management Console Exception)

WSO2 ESB 中介序列與代理服務 (WSO2 ESB Mediation Sequence vs Proxy Service)

帶有 XPath 擴展的 WSO2 Developer Studio (WSO2 Developer Studio with XPath extensions)

我們可以使用 wso2esb Jms 消費消息嗎 (Can we Consume the Messages using wso2esb Jms)

使用 WSO2 peoplehr 連接器時出錯 (Error using WSO2 peoplehr connector)

在路徑中找不到 WSO2ESB 自定義中介 Java 類 (WSO2ESB Custom Mediator Java Class is not found in the path)

WSO2 豐富陣列 (WSO2 Enrich array)

wso2代理服務出錯 (Error in wso2 proxy service)

如何豐富 OM 屬性中的節點值? (How to enrich a node value in OM property?)

如何發送字符串內容作為來自 WSO2 ESB 代理的響應? (How to send string content as the response from WSO2 ESB Proxy?)

wso2-esb 集群後,每個 carbon 控制台(管理和工作人員)都不工作 (wso2-esb After clustering, every carbon console(management and worker) doesn't work)

WSO2 ESB:如何處理端點返回的內部錯誤 (WSO2 ESB: How to handle internal error returned by an endpoint)







留言討論