mule - creating an XML attribute from var in dataweave 2.0
问题描述
I have a JSON input:
{
abc: "",
def: "hello"
}
I want to make this blank element as nillable in XML i.e. . I am using the below dataweave code:
%dw 2,0
output application/xml skipNullOn="everywhere"
var makeNil= (in) ->
in match {
case is Array -> in map makeNil($)
case is Object -> in mapObject (
if ( ($) == "")
($$) @(xsi#'nil':true): {}
else ($$): makeNil($)
)
else -> in
}
---
makeNil(payload)
I am not able to create an attribute using @(xsi#'nil':true) for key($$). Please help me
解决方案
解决我在评论中提到的错误,添加根元素是可行的。请记住,与 JSON 不同,XML 需要一个根元素。
%dw 2.0
output application/xml skipNullOn="everywhere"
ns xsi http://www.w3.org/2001/XMLSchema-instance
var makeNil= (in) ->
in match {
case is Array -> in map makeNil($)
case is Object -> in mapObject (
if ( ($) == "")
($$) @(xsi#'nil':true): {}
else ($$): makeNil($)
)
else -> in
}
---
top: makeNil(payload)
输入:
{
"abc": "",
"def": "hello"
}
输出:
<?xml version='1.0' encoding='UTF-8'?>
<top>
<abc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<def>hello</def>
</top>
推荐阅读
- python - 我可以在python中将这两个几乎相同的函数合二为一吗?
- css - Bootstrap 4.0 下拉菜单未使用导航栏的完整高度
- php - 这个变量是如何从另一个引用中调用的
- excel - 跨工作簿错误使用转置功能
- serialization - 如何调试保存模型类型错误:不能腌制 SwigPyObject 对象?
- javascript - 在 iframe 中设置超时
- jquery - Jquery排序和分页表不起作用
- python - 使用 tf.keras.models.load_model 恢复保存的模型时,Tensorflow 2.0+ 损失字典中的未知条目
- xml - 三星电视遥控器通过 UPnP
- html - Stargazer Table 未计算所有描述性值