在Mule 4 Dataweave for XML中同时添加多个转换
我正在编写一个将XML格式的数据发送到第三方客户机的应用程序。有34个子流需要我编写,我想把重点放在将转换分解成可以重用的代码上 这就是我发送给第三方的交易的样子:在Mule 4 Dataweave for XML中同时添加多个转换,xml,mule,dataweave,Xml,Mule,Dataweave,我正在编写一个将XML格式的数据发送到第三方客户机的应用程序。有34个子流需要我编写,我想把重点放在将转换分解成可以重用的代码上 这就是我发送给第三方的交易的样子: <request> <control> <senderid>{{sender_id}}</senderid> <password>{{sender_password}}</password> <controlid>{{$t
<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>
认证:
内容(示例):
我无法将“内容”设置为“Auth”之后的节点,并嵌套在最终转换的“根”中
我如何做到这一点,这样我就只需要引用“Root”和“Auth”,并在流中实际设置“Content”?如果有其他解决方案,则不必在Dataweave中进行转换
预期的输出是,我可以调用Root和Auth,然后在其他转换中专门为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脚本来一次创建整个输出,或者像以前那样单独创建部分,但将输出编写为应用程序/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转换为JSON时,必须定义如何处理标记属性请提供预期的输出?提前谢谢
所以我将JSON拆分为您所指的JSON?@aled我问题中的JSON是我需要发送的XML的JSON格式版本。我将它分为“Root”和“Auth”两部分,“output”设置为“application/xml”。我将“Root”和“Auth”分开,因为“Root”最初用于生成“Auth”变量“sessionid”,这意味着它更易于重用。@aplan1290为什么不创建一个自定义模块呢?()您还可以创建自定义函数,只需调用它们并组合对象。例如,++操作符可以将两个数据结构组合成一个。
%dw 2.0
output application/json
---
{
request: {
auth: vars.auth,
...
%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
}
}
<content>
<function controlid="{{$guid}}">
<read>
<object>ARPYMT</object>
<keys>1</keys>
<fields>*</fields>
</read>
</function>
</content>
<?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>
{
"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": "*"
}
}
}
}
}