"How to group xml data in Mule3 anypoint studio"

"How to group xml data in Mule3 anypoint studio"

我正在做从 json 到 xml 的转换,然后必须按部门分组。 我正在使用 mysql 数据库来获取员工及其部门。在最后一个转换器中,我必须按部门分组。

这是我的 json 转换后从数据库中得到的。

[
    {
        "id": 1,
        "Full Name": "Devendra",
        "Department": {
            "DepartmentName": "IT"
        }
    },
    {
        "id": 2,
        "Full Name": "Rohit",
        "Department": {
            "DepartmentName": "IT"
        }
    }
]

然后,我正在进行 json 到 xml 的转换并得到以下结果。

<?xml version="1.0" encoding="windows-1252"?>
<employees>
    <employee>
        <id>1</id>
        <FullName>Devendra</FullName>
        <Department>
            <DepartmentName>IT</DepartmentName>
        </Department>
    </employee>
    <employee>
        <id>2</id>
        <FullName>Rohit</FullName>
        <Department>
            <DepartmentName>IT</DepartmentName>
        </Department>
    </employee>
</employees>

预期结果: 我需要按部门动态显示结果组,如下所示-

<?xml version="1.0" encoding="windows-1252"?>
<Departments>
    <Department>
        <DepartmentName>IT</DepartmentName>
        <employee>
            <id>1</id>
            <FullName>Devendra</FullName>
        </employee>
        <employee>
            <id>2</id>
            <FullName>Rohit</FullName>
        </employee>
    </Department>
</Departments>

我该如何进行这种转换?

json2xml:

%dw 1.0
%output application/xml
---
Departments: payload groupBy $.Department.DepartmentName mapObject {
    Department: 
        {DepartmentName: $$} ++
        {($ map {
            employee: {
                id: $.id,
                FullName: $.'Full Name'
            }
        })
    }
}

xml2xml:

%dw 1.0
%output application/xml
---
Departments: payload.employees groupBy $.Department.DepartmentName mapObject {
    Department: 
        {DepartmentName: $$} ++
        {($ map {
            employee: {
                id: $.id,
                FullName: $.FullName
            }
        })
    }
}