【发布时间】:2021-06-11 06:28:39
【问题描述】:
我正在编写一个将 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>
我的想法是我可以将它们分成单独的变换并将它们加在一起。我认为我不能在 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")
}
}
}
授权:
%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
}
}
}
}
我无法将“内容”设置为“身份验证”之后的节点并嵌套在最终转换的“根”中。
我将如何做到这一点,所以我只需要引用“根”和“身份验证”,并且实际上会在我的流程中设置“内容”?如果有其他解决方案,它不一定是 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 中是不可能的。
【问题讨论】:
-
您能提供预期的输出吗?提前致谢!
-
so I've split the JSON out你指的是什么 JSON? -
@aled 我的问题中的 JSON 是我需要发送的 JSON 格式的 XML 版本。我将它分为“Root”和“Auth”部分,“output”设置为“application/xml”。而且我已经将“Root”与“Auth”分开了,因为“Root”最初用于生成“Auth”变量“sessionid”,这意味着它更具可重用性。
-
@aplane1290 为什么不创建自定义模块? (docs.mulesoft.com/mule-runtime/4.3/dataweave-create-module) 您还可以创建自定义函数并调用它们并组合您的对象。例如,++ 运算符可以将两个数据结构合二为一。