Read XML with namespace in dataweave

172 views Asked by At

I am trying to read an xml and put if else conditions on a certain tag. If data exists, I need to print true otherwise false. Below is the sample XML that I am trying to read:

<?xml version='1.0' encoding='ISO-8859-1'?>
<ns01:active xmlns:ns01="http://Atlas.Schemas.Policy/1.0">
  <Id>
    <groupId>66d9057f-0951-4970-86d6-4d69d3572abe</groupId>
  </Id>
</ns01:active>

I tried using this dataweave code but it does not work me:

 if(payload.active.Id.groupId != null) "true" else "False"
1

There are 1 answers

1
aled On

The issue you are having is unrelated to the usage of a namespace. Note that there is no way to represent null in plain XML.

If you want to return true if the element exists (ie there is a key with that name) independent of its value (ie the content inside the element) then you can use the key-present selector (ie add a ? question mark after the key name).

If instead you want to see if the value of the element is empty you could use the function isEmpty(). You can negate its return value using the not operator (!).

Example:

%dw 2.0
output application/json
---
{
    keyPresentSelector: payload.active.Id.groupId?,
    hasValue: !isEmpty(payload.active.Id.groupId)
}

Output:

{
  "keyPresentSelector": true,
  "hasValue": true
}