Reputation: 531
In Mulesoft, I need to use Transform component to extract value as String from XML payload ,If I dont write output/plain , it will throw xml parsing error. BTW, I use Transform component to transform xml payload to json and extract value from xml payload and set as variable
Input XML
<?xml version='1.0' encoding='UTF-8'?>
<TABLES>
<person_ID>12345</person_ID>
</TABLES>
Transform Component code
<ee:transform>
<ee:variables>
<ee:set-variable variableName="person_ID" >
<![CDATA[payload.TABLES.person_ID]]>
</ee:set-variable>
</ee:variables>
</ee:transform>
Error Message
"Trying to output non-whitespace characters outside main element tree (in prolog or epilog), while writing Xml at ." evaluating expression: "payload.TABLES.person_ID".
If I use output text/plain, it can extract person_ID, but not as String. (because without qoutations)
Any idea or suggestion are more than welcome. I am new bee for Mulesoft.
%dw 2.0
output text/plain
---
payload.TABLES.person_ID default null
Upvotes: 0
Views: 469
Reputation: 25837
I'll assume the input is similar to this:
<TABLES>
<person_ID>abc</person_ID>
</TABLES>
The error you are having appears to be that since your script doesn't explicitly set an output format then DataWeave is inferring automatically it to XML based on the input. A single string can not be a valid XML hence the error.
To simply get a string that you are going to use later in the flow you should use application/java
as the output format. You could also use JSON or text/plain
. It doesn't matter if you don't see the quotes explicitly if DataWeave understand it is a string type. Using JSON makes the quotes explicitly visible but it means also means that for using the value it has to be parsed again. Using Java for the output doesn't require additional parsing so it is more performant.
Example:
%dw 2.0
output application/java
---
payload.TABLES.person_ID default null
Output:
abc
Upvotes: 1