Yatan
Yatan

Reputation: 93

How to read space in XML field value using DataWeave

I have a XML input and in one of the field its having space, using dataweve its treating space as null, I want to read the space as character.

XML Input:

<employee>
 <id>123</id>
 <name> </name>
</employee>

DataWeave script:

%dw 2.0
output application/xml
---
"root" : {
  "id" : payload.employee.id, 
  "name" : if (payload.employee.name == " ") "space" else payload.employee.name 
}

The output is coming as below:

<?xml version='1.0' encoding='UTF-8'?>
<root>
  <id>123</id>
  <name/>
</root>

Upvotes: 0

Views: 89

Answers (2)

Sandesh
Sandesh

Reputation: 1220

Try below script :

%dw 2.0
output application/xml 

---
"root" : {
  "id" : payload.employee.id, 
  "name" : if (payload.employee.name is Null) "space" else payload.employee.name 
}

enter image description here

Upvotes: 0

aled
aled

Reputation: 25812

You need to set the reader property nullValueOn (XML reader properties documentation) to "empty" or "none" in the outputMimeType attribute of the component where the payload is created so when it is parsed the blanks are not replaced by a null.

For example if the payload is coming from a <file:listener>:

<file:listener doc:name="On New File" config-ref="File_Config" 
      outputMimeType='application/xml; nullValueOn="empty"'>

Note the use of single quotes around the value of outputMimeType to avoid conflicting with the double quotes used by nullValueOn.

Upvotes: 1

Related Questions