【问题标题】:Adding multiple Transforms together in Mule 4 Dataweave for XML在 Mule 4 Dataweave for XML 中添加多个转换
【发布时间】: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) 您还可以创建自定义函数并调用它们并组合您的对象。例如,++ 运算符可以将两个数据结构合二为一。

标签: xml mule dataweave


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    您可以使用以下 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 时,您必须定义如何处理标记属性

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多