基于条件的Mule中的数组到平面映射

问题描述 投票:5回答:3

我试图使用Mule中的DataMapper变换将第三方调用的响应映射到不同的结构。

从第三方开始,我们收到一系列项目(以及其他内容),我想将数组中的单个项目映射到对象(JSON)。我收到请求中项目的标识符,该标识符可用作输入参数。

我的问题是,如何根据标识符映射项目的字段?

示例XML响应

<account>
    <accountNumber>1234567</accountNumber>
    <books>
        <book>
            <id>1</id>
            <title>Sample title1</title>
            <availableFrom>21-03-16</availableFrom>
        </book>
        <book>
            <id>2</id>
            <title>Sample title2</title>
            <availableFrom>21-03-16</availableFrom>
        </book>
        <book>
            <id>3</id>
            <title>Sample title3</title>
            <availableFrom>21-03-16</availableFrom>
        </book>
    </books>
</account>

需要成为:

{
    "person": {
        "accountNumber": 1234567,
        "selectedBook": {
            "id": 1, 
            "title": "Sample title1"
        },
        "otherBooks": [
            {
                "id": 2, 
                "title": "Sample title2"
            },
            {
                "id": 3, 
                "title": "Sample title3"
            }
        ]
    }
}

所选书籍的id以inputArgument.bookId保存。

我可以使用xpath规则为每个字段完成映射,但是,我必须在xpath中对bookId进行硬编码。相反,我需要能够将实际的id替换为所提供的id(并在inputArgument中可用)。

标题的xpath

/account/books/book[child::id=1]/title

我已经尝试添加MEL来替换id和各种其他修复,但似乎没有任何工作。有没有办法替换bookId字段。

注意:由于客户端的限制,我无法使用DataWeave。

xpath mule mule-el
3个回答
-1
投票
In Case of Mule in case of single XML element convert to Array 

xml files : 

<usrId>10120</usrId>

IN Rule file use double dot($..usrId) instead ofsinfle ($.usrId) it will revert array element for single element.

RULE FILE:
<parameter parameter_name_client="$..usrId"
parameter_name_server="facture_ids" format_required="false"
data_type="Array" value_change_required="false"
is_TLV="false" is_mandatory="false" to_be_logged="false"/>
© www.soinside.com 2019 - 2024. All rights reserved.