簡體   English   中英

在 dataweave 2.0 中從 var 創建 XML 屬性

[英]creating an XML attribute from var in dataweave 2.0

我有一個 JSON 輸入:

{
  abc: "",
  def: "hello"

}

我想讓這個空白元素在 XML 中可以為 nillable ie 。 我正在使用以下 dataweave 代碼:

%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)

我無法使用 @(xsi#'nil':true) 為 key($$) 創建屬性。 請幫我

解決我在評論中提到的錯誤,添加根元素有效。 請記住,與 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM