温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - How do you convert SQL Server SELECT into XML in Mule 4?
dataweave mule mulesoft sql-server xml

其他 - 如何在Mule 4中将SQL Server SELECT转换为XML?

发布于 2020-04-26 10:02:25

如何使用Mule 4中的Dataweave将以下SQL输出转换为XML?

SELECT s.RefId
          ,s.LocalId
          ,s.StateProvinceId
          ,s.SchoolName
          ,e.Email
          ,e.EmailType
      FROM SchoolInfo s
      LEFT OUTER JOIN SchoolEmail e
      ON    e.SchoolRefId = s.RefId
      WHERE s.RefId = :ref_id

SQL的输出为:

RefId                               LocalId StateProvinceId SchoolName      Email               Type
7FDF722B-6BBA-4BF0-8205-A5380B269EF1    1   SA              Steve's School  steven@gmail.com    prm
7FDF722B-6BBA-4BF0-8205-A5380B269EF1    1   SA              Steve's School  test@gmail.com      sec

XML输出应如下所示:

<ns0:SchoolInfo xmlns:ns0="http://www.sifassociation.org/datamodel/au/3.4" RefId="7FDF722B-6BBA-4BF0-8205-A5380B269EF1">
  <ns0:LocalId>1</ns0:LocalId>
  <ns0:StateProvinceId>SA</ns0:StateProvinceId>
  <ns0:SchoolName>Steve's School</ns0:SchoolName>
  <ns0:SchoolEmailList>
    <ns0:Email Type="prm">steven@gmail.com</ns0:Email>
    <ns0:Email Type="sec">test@gmail.com</ns0:Email>
  </ns0:SchoolEmailList>
</ns0:SchoolInfo>

谢谢,史蒂夫

查看更多

提问者
Steve
被浏览
24
George 2020-02-10 22:09

这是将生成相同XML的DW表达式:

%dw 2.0
output application/xml
ns ns0 http://www.sifassociation.org/datamodel/au/3.4
var rId = payload[0].RefId
var lId = payload[0].LocalId
var sId = payload[0].StateProvinceId
---
ns0#SchoolInfo @(RefId: rId): {
    ns0#LocalId: lId,
    ns0#StateProvinceId: sId,
    ns0#SchoolEmailList: payload reduce (e,acc={}) -> acc ++ {
        ns0#Email @(Type: e.Type): e.Email
    }   
} 

我假设RefIdLocalId以及StateProvinceId永远是每个查询相同。

Explanation of reduce: reduce is explained here in detail along with its theoretical foundations. Here's also reduce's MuleSoft documentation page. This last page does a pretty good job explaining reduce

Now in my own words, reduce takes as an input (1) an array and (2) a lambda function.

The array contains the elements reduce will iterate over in a similar fashion as a map function does. The similarities between the map and reduce functions ends here :).

The lambda function expects two arguments: (1) the current element you iterate from the array and (2) the accumulator. The accumulator can be initialized into a value (I set it to an object {} in your use-case because XML does not like arrays). The result of the lambda function for this FIRST iteration is set as the accumulator for the next iteration and so on.

The result of reduce is the accumulator once iterating over the array is done.

Thus, if I was to trace this specific reduce it will look something like this and I simplify the denotation of these values:

/*
 * 1st iteration: (e=steven@gmail.com, acc={}) -> acc + {Email: steven@gmail.com}
 * 2nd iteration: (e=test@gmail.com, acc={Email: steven@gmail.com} -> acc + {Email: test@gmail.com}
 * result: acc = {Email: steven@gmail.com, Email: test@gmail.com}
 */