Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error trying to remove a namespace from an XML in Mule 4

I see the error like "Trying to write END_DOCUMENT when document has no root" while I replace all the payload env and xmlns with "".

It throws error :

Message               : "Trying to write END_DOCUMENT when document has no root (ie. trying to output empty document, while writing Xml.
Trace:
  at main (Unknown)" evaluating expression: "payload.replaceAll("env:","").replaceAll("xmlns=\"http://decisionresearch.com/RateMaker\"","")".
Element               : map_requestFlow/processors/0 @ map_request:map_request.xml:13 (Set Payload)
Element DSL           : <set-payload value="#[payload.replaceAll("env:","").replaceAll("xmlns=\"http://decisionresearch.com/RateMaker\"","")]" doc:name="Set Payload" doc:id="7db57e88-dbd4-4a09-ba05-ea37fb9586fc"></set-payload>
Error type            : MULE:EXPRESSION
FlowStack             : at map_requestFlow(map_requestFlow/processors/0 @ map_request:map_request.xml:13 (Set Payload))

  (set debug level logging or '-Dmule.verbose.exceptions=true' for everything) 

Mule flow :

<flow name="map_requestFlow" doc:id="21d0d652-3766-47ed-95aa-a451f62c5776" >
        <http:listener doc:name="Listener" doc:id="7312114b-2857-40b3-98cd-f52b628b3a28" config-ref="HTTP_Listener_config" path="/map"/>
        <set-payload value='#[payload.replaceAll("env:","").replaceAll("xmlns=\"http://decisionresearch.com/RateMaker\"","")]' doc:name="Set Payload" doc:id="7db57e88-dbd4-4a09-ba05-ea37fb9586fc" />
        <logger level="INFO" doc:name="Logger" doc:id="21051f39-bb9f-412c-af73-f65051317757" message="#[payload]"/>
</flow>

Input payload : https://github.com/Manikandan99/Map_request/blob/main/soap_request.xml

Expected output : https://github.com/Manikandan99/Map_request/blob/main/soap_response.xml

I am trying to replace payload using

#[payload.replaceAll("env:","").replaceAll("xmlns=\"http://decisionresearch.com/RateMaker\"","")]

Please assist me to resolve this issue.

like image 335
codey_08 Avatar asked Dec 07 '25 19:12

codey_08


1 Answers

If you use string operations to work with XML, chances are you are doing it wrong. In other programming languages you should use the existing libraries and frameworks to process XML. In Mule you should use DataWeave transformations. This one is a little bit intuitive because it requires using the seldom used namespace selector. The selector returns the namespace URL, not the name. I created this simple function to remove a namespace from the output:

%dw 2.0
output application/xml
fun removeNamespace(element,namespaceName) =
  element mapObject (value, key) -> 
            (if (key.# as String == namespaceName) (key as String) else (key)) @((key.@)) : (
                if (value is Object) removeNamespace(value, namespaceName)                             else value
            )
---
removeNamespace(payload, "http://schemas.xmlsoap.org/soap/envelope/")
like image 154
aled Avatar answered Dec 10 '25 12:12

aled