在Mule 4 Dataweave for XML中添加多个Transform



我正在编写一个应用程序,该应用程序将向第三方客户端发送XML格式的数据。有34个子流需要我编写,我想把重点放在将Transforms拆分为可重用的代码上。

这就是我发送给第三方的交易的样子:

<request>
<control>
<senderid>{{sender_id}}</senderid>
<password>{{sender_password}}</password>
<controlid>{{$timestamp}}</controlid>
<uniqueid>false</uniqueid>
<dtdversion>3.0</dtdversion>
<includewhitespace>false</includewhitespace>
</control>
<operation>
<authentication>
<sessionid>{{temp_session_id}}</sessionid>
</authentication>
<content>
<function controlid="{{$guid}}">
---CONTENT---
</function>
</content>
</operation>
</request>

我的想法是,我可以将它们分离成单独的Transform,并将它们添加在一起。我不认为我可以在Mule 4中显式地编写XML,所以我将JSON单独拆分为以下调用,并依赖Dataweave的XML翻译:

根:

%dw 2.0
output application/xml
---
{
"request":
{
"control":
{
"senderid": Mule::p("secure::finance.sender.id"),
"password" : Mule::p("secure::finance.sender.password"),
"controlid" : now(),
"uniqueid": Mule::p("secure::finance.uniqueid"),
"dtdversion" : Mule::p("secure::finance.dtdversion"),
"includewhitespace": Mule::p("secure::finance.includewhitespace")
}
}
}

Auth:

%dw 2.0
output application/xml
---
{
"operation":
{
"authentication":
{
"login":
{
"sessionid" : vars.sessionId 
}
}
}
}

内容(示例(:

"content":
{
"function controlid":
{
"create_ictransaction":
{
"datecreated": 
{
"year": now().year,
"month": now().month,
"day": now().day
}
}
}
}

我无法设置";"内容";作为"节点"之后的节点;Auth";并嵌套在";根";我最后一次变身。

我该如何做到这一点,所以我只需要参考";根";以及";Auth";并且实际上将设置";"内容";在我的流动中?如果有其他解决方案的话,它不一定是Dataweave中的Transforms。

预期的输出是,我可以调用Root和Auth,然后在其他Transform中为XML专门定义JSON。这将有助于缩短代码的长度。

像这样:

[Root]
[Auth]
"content":
{
"function controlid":
{
"create_ictransaction":
{
"datecreated": 
{
"year": now().year,
"month": now().month,
"day": now().day
}
}
}
}
[Root ending to encapsulate call]

编辑:我认为这在Dataweave中是不可能的。

您可以创建一个DataWeave脚本来一次创建整个输出,也可以像您所做的那样单独创建部分,但将输出写入application/java并将每个部分发送到一个变量。然后您可以使用这些值来组成总输出。

示例:如果您将Auth部分存储在Auth变量中:

%dw 2.0
output application/json
---
{
request: {
auth: vars.auth,
...

您可以使用以下DataWeave表达式来实现这一点:

%dw 2.0
output application/xml //replace with application/json for a JSON output
---
{
"request": {
"control": {
"senderid": 'sid1', // replace with Mule::p("secure::finance.sender.id"),
"password" : 'somepassword', // replace with Mule::p("secure::finance.sender.password"),
"controlid" : now(),
"uniqueid": "someuid", // replace with Mule::p("secure::finance.uniqueid"),
"dtdversion" : "somedtdver", // replace with Mule::p("secure::finance.dtdversion"),
"includewhitespace": "true" // replace with Mule::p("secure::finance.includewhitespace")
},
"operation": {
"authentication": {
"login": {
"sessionid" : '123' //replace with vars.sessionId
}
}
},
"content": payload.content
}   
}

给定以下XML输入有效载荷:

<content>
<function controlid="{{$guid}}">
<read>
<object>ARPYMT</object>
<keys>1</keys>
<fields>*</fields>
</read>
</function>
</content>

结果输出有效载荷为:

XML输出:

<?xml version='1.0' encoding='UTF-8'?>
<request>
<control>
<senderid>sid1</senderid>
<password>somepassword</password>
<controlid>2021-06-10T20:52:08.225041Z</controlid>
<uniqueid>someuid</uniqueid>
<dtdversion>somedtdver</dtdversion>
<includewhitespace>true</includewhitespace>
</control>
<operation>
<authentication>
<login>
<sessionid>123</sessionid>
</login>
</authentication>
</operation>
<content>
<function controlid="{{$guid}}">
<read>
<object>ARPYMT</object>
<keys>1</keys>
<fields>*</fields>
</read>
</function>
</content>
</request>

JSON输出:

{
"request": {
"control": {
"senderid": "sid1",
"password": "somepassword",
"controlid": "2021-06-10T20:54:25.568852Z",
"uniqueid": "someuid",
"dtdversion": "somedtdver",
"includewhitespace": "true"
},
"operation": {
"authentication": {
"login": {
"sessionid": "123"
}
}
},
"content": {
"function": {
"read": {
"object": "ARPYMT",
"keys": "1",
"fields": "*"
}
}
}
}
}

注意:当将XML转换为JSON时,您必须定义如何处理标记属性

最新更新