我尝试通过检查变量的null并在结果中设置来实现一个简单的循环。
我的骡子流部分如下所示。
数据库(选择) - >转换消息(DataWeave-转换为xml)
数据库的价值如下:
Book1 Book2 Book3 Book4 Book5
abc cde fgh ijk lmn
opq rst NULL NULL NULL
Dataweaver代码如下所示:
ns0#LibraryDetails:{
ns0#User: payload."Book1",
ns0#User: payload."Book2",
ns0#User: payload."Book3",
ns0#User: payload."Book4",
ns0#User: payload."Book5"
}
预期的输出1是
<LibraryDetails>
<User>abc</User>
<User>cde</User>
<User>fgh</User>
<User>ijk</User>
<User>lmn</User>
</LibraryDetails>
预期的输出2是
<LibraryDetails>
<User>opq</User>
<User>rst</User>
</LibraryDetails>
我们如何才能仅转义空值并在数据编织中创建循环。我也试过了地图功能。你能否就此展示一些亮点。
答案 0 :(得分:1)
要获得预期输出,请使用map
,然后使用skipNullOn="everywhere"
,如果您希望所有元素都使用该逻辑。以下给出了您的预期输出
%dw 1.0
%output application/xml skipNullOn="everywhere"
%namespace ns0 http://something.com
---
ns0#LibraryDetails: { (payload map {
ns0#User: $.Book1,
ns0#User: $.Book2,
ns0#User: $.Book3,
ns0#User: $.Book4,
ns0#User: $.Book5
} ) }
答案 1 :(得分:0)
你可以试试这个:
ns0#LibraryDetails:{
ns0#User: when null != payload."Book1" then payload."Book1" ,
ns0#User: when null != payload."Book2"then payload."Book2",
ns0#User: when null != payload."Book3"then payload."Book3",
ns0#User: when null != payload."Book4"then payload."Book4",
ns0#User: when null != payload."Book5"then payload."Book5"
}
ELSE: 使用beow语句: %output application / xml skipNullOn =“everywhere”