You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSO2 Foreach Mediator多固定长度JSON转XML问题求助

WSO2 ESB Foreach Mediator处理多条目JSON转XML时字段为空的解决方案

问题描述

作为WSO2新手,单条固定长度JSON转XML已成功,但使用Foreach Mediator处理多条时,转换后的XML字段全部为空。

原API代码:

<?xml version="1.0" encoding="UTF-8"?>
<api context="/dm4" name="DataMapper4" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <foreach expression="//InformationBankAccount/Accounts" id="itr1">
                <sequence>
                    <property expression="json-eval($.InformationBankAccount.Accounts.InputInformation)" name="Info" scope="default" type="STRING"/>
                    <payloadFactory media-type="xml">
                        <format>
                            <Accounts xmlns="">
                                <NumberAccount>$1</NumberAccount>
                                <AccountName>$2</AccountName>
                                <AccountPhoneNumber>$3</AccountPhoneNumber>
                            </Accounts>
                        </format>
                        <args>
                            <arg evaluator="xml" expression="substring($ctx:Info,1,8)"/>
                            <arg evaluator="xml" expression="translate(substring($ctx:Info,9,10),' ','')"/>
                            <arg evaluator="xml" expression="translate(substring($ctx:Info,19,12),' ','')"/>
                        </args>
                    </payloadFactory>
                </sequence>
            </foreach>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

PostMan返回结果:

<jsonObject>
    <InformationBankAccount>
        <Accounts>
            <NumberAccount/>
            <AccountName/>
            <AccountPhoneNumber/>
        </Accounts>
        <Accounts>
            <NumberAccount/>
            <AccountName/>
            <AccountPhoneNumber/>
        </Accounts>
    </InformationBankAccount>
</jsonObject>

原请求JSON(格式错误):

{
      "InformationBankAccount":{
         "Accounts":
            {
                "InputInformation":"10082022HitoshiNAG085688001530"
            },
            "Accounts":
            {
                "InputInformation":"10099999HitoshiNAG085688001530"
            }
      }
}

问题原因

  1. 请求JSON格式错误:JSON中重复的Accounts键会被解析器覆盖,实际只会保留最后一个条目,导致Foreach处理无效数据。
  2. 缺少JSON转XML步骤:未显式执行<json-to-xml/>,Foreach的XPath表达式无法正确匹配XML节点。
  3. Foreach上下文引用错误:循环内部使用全局JSON路径json-eval($.InformationBankAccount.Accounts.InputInformation),无法获取当前循环条目的数据。
  4. XPath表达式错误:原Foreach表达式//InformationBankAccount/Accounts未包含JSON转XML后的根节点jsonObject,无法定位目标节点。

解决方案

1. 修正请求JSON格式

将重复的Accounts改为数组形式:

{
  "InformationBankAccount": {
    "Accounts": [
      {
        "InputInformation": "10082022HitoshiNAG085688001530"
      },
      {
        "InputInformation": "10099999HitoshiNAG085688001530"
      }
    ]
  }
}

2. 修正API代码

<?xml version="1.0" encoding="UTF-8"?>
<api context="/dm4" name="DataMapper4" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <!-- 显式将JSON转换为XML,确保结构正确 -->
            <json-to-xml/>
            <!-- 修正XPath表达式,包含根节点jsonObject -->
            <foreach expression="//jsonObject/InformationBankAccount/Accounts" id="itr1">
                <sequence>
                    <!-- 从当前循环的Accounts节点提取InputInformation -->
                    <property expression="//InputInformation" name="Info" scope="default" type="STRING"/>
                    <payloadFactory media-type="xml">
                        <format>
                            <Accounts xmlns="">
                                <NumberAccount>$1</NumberAccount>
                                <AccountName>$2</AccountName>
                                <AccountPhoneNumber>$3</AccountPhoneNumber>
                            </Accounts>
                        </format>
                        <args>
                            <arg evaluator="xml" expression="substring($ctx:Info,1,8)"/>
                            <arg evaluator="xml" expression="translate(substring($ctx:Info,9,10),' ','')"/>
                            <arg evaluator="xml" expression="translate(substring($ctx:Info,19,12),' ','')"/>
                        </args>
                    </payloadFactory>
                </sequence>
            </foreach>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

关键修改说明

  • 添加<json-to-xml/>:确保JSON被正确转换为XML结构,为XPath匹配提供基础。
  • 修正Foreach的XPath://jsonObject/InformationBankAccount/Accounts,正确定位到数组节点。
  • 调整属性提取方式:在循环内部直接通过//InputInformation获取当前条目的数据,而非全局引用JSON路径。

内容的提问来源于stack exchange,提问作者Hitoshi Mario Naga Mait

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 02:03:20