简体   繁体   中英

Dataweave Mulesoft Skip XML tags

I am using dataweave to transform a csv file to xml file. At some places there is no value for a certain xml tag and I am getting an empty tag.

What I want is if there is no value than that tag must not be present in xml at all.

It's a long mapping so I don't want to write the entire mapping in the when otherwise tag but just the mapping for that tag in the when otherwise condition.

Data mapping

    name: ((payload01.name) when payload01.name != ""
    otherwise {}),

Output: If there is value for name in input

    <name>Kittu</name>

Otherwise

    <name/> 

What I want is that in the otherwise condition, no tag should come at all.

All help is appreciated. Thanks.

"name:" should be inside parenthesis, like this:

(name: (payload01.name) when payload01.name != ""
         otherwise {})

Use skipNullOn="everywhere" on output declaration so that you need not declare it for all the elements.

%output application/xml skipNullOn="everywhere"

Reference: https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation#output-directive

If you need the empty tags to be present, then we will have to use when otherwise as shown below:

name: payload01.name when ((payload01.name != null) and (payload01.name != "" )) otherwise "",

other way of doing it is specifying 'default' value which is the simplest:

name: payload01.name default "",

Agree with Victor P, "name:" should be inside the parentheses. However, to get the same result you can use the following snippet.

(name: payload01.name) when payload01.name != ""

Although without otherwise it will construct the name, unless the payload01.name is empty.

Use skipNullOn="attributes" on output declaration so that you need not declare it for all the elements.

%output application/xml skipNullOn="attributes"

For adding empty tab in mule this should more appropriate. name: payload.name default {}

this will produce if the name don't have a value.

skipNullOn checks if elements or attributes are present or not. eg If name element is not present in your input, then it will not show it in output.

To check if value is empty, you will have to use a condition like this: (name: payload01.name) when payload01.name != ""

If you use skipNullOn="everywhere" tag will be there in output field eventhough the it has no value . So it won't work in this case. Try writing seperate function for checking and skipping the null values with that entire tag.

You can put a check as below to send a xml tag/s :

*(Contact: {
                mobNo: payload.moNo,
                countryCode: payload.countryCode
            }
) when payload != null and paylaod.mobNo != null*

By above whole Cntact tag will go in the request only if there is valid mobNo present in the payload.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM