1
votes

I want check if a Node is present in an XML payload or not. For this purpose I want to use xpath3() in Transform Message in mule4.

Input:

<XML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ABC xsi:nil="true"/>
    <DEF/>
</XML>

dataweave code:

%dw2.0
output application/json skipNullOn="everywhere"
---
if(xpath3('//:ABC',payload.XML,'BOOLEAN')
 "abc": "yes
else
 "abc": "no" 

Expected Output( json):

{
"abc": "yes"
}

Please Help me with this.

2

2 Answers

2
votes

Any reason why use xpath? You could achieve the same result using the following dataweave expressions:

  1. Check if element exists (whether it's nil or not)
%dw 2.0
output application/json skipNullOn="everywhere"
---
{
    "abc": if (payload.XML.ABC?) "yes" else "no"
}
  1. Check if element is present and not nil (nil must be set to false or removed when ABC has a value):
%dw 2.0
output application/json skipNullOn="everywhere"
---
{
    "abc": if (!isEmpty(payload.XML.ABC)) "yes" else "no"
}

Example of output using these expressions:

Output

1
votes

DataWeave 2.x (in Mule 4.x) does not have an xpath3() function. If for some reason you want to use XPATH inside DataWeave you will have to develop the function yourself, though I would advise against doing it.

The recommended way is to migrate it to a DataWeave expression, like @olamiral solution.