3

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.

aled
  • 21,330
  • 3
  • 27
  • 34
codey_08
  • 249
  • 1
  • 11
  • what exactly you want to replace,its not clear form your question, Could you please let us know in comment or edit lil bit your post? or what is your expected o/p? – Anurag Sharma Feb 07 '22 at 15:39
  • please let us know what is the exact final requirement. I see that you are getting SOAP request and are you trying to remove the namespace ? I think what you are trying to do is manual work. If you give us the exact end to end requirement then it is easy to help – Harsha Shivamurthy Feb 07 '22 at 15:47
  • i'm trying replace all the namespace with "". – codey_08 Feb 07 '22 at 15:55
  • @AnuragSharma,@Harsha..now I updated with expected output. – codey_08 Feb 07 '22 at 16:09

3 Answers3

5

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/")
aled
  • 21,330
  • 3
  • 27
  • 34
3

Here's a version that uses the namespace prefix instead of the url.

%dw 2.0
output application/xml 

fun removeNamespace(element,namespacePrefix) =
    element mapObject (value, key) -> {
    //key
    (
        if (key.#.prefix as String == namespacePrefix) (key as String) 
        else (key)) @((key.@)
    )
    : //value
    (
        if (value is Object) removeNamespace(value, namespacePrefix)
        else value
    )}
---
payload removeNamespace  "env"
Ethan Port
  • 166
  • 8
2

Try the below transformation in DataWeave.

%dw 2.0
output application/xml  
---

  "Envelope" @("xmlns:env": "http://schemas.xmlsoap.org/soap/envelope/", "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", "xmlns:xsd": "http://www.w3.org/2001/XMLSchema"): {
    "Body": {
      rateResponse: payload.Envelope.Body.rateResponse
    }
  }