尝试从 Mule 4 中的 XML 中删除名称空间时出错

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

当我用“”替换所有负载环境和 xmlns 时,我看到类似“尝试写入 END_DOCUMENT 当文档没有根目录”这样的错误。

它抛出错误:

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) 

骡子流:

<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>

输入负载:https://github.com/Manikandan99/Map_request/blob/main/soap_request.xml

预期输出:https://github.com/Manikandan99/Map_request/blob/main/soap_response.xml

我正在尝试使用

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

请帮我解决这个问题。

如果您使用字符串操作来处理 XML,很可能您做错了。在其他编程语言中,您应该使用现有的库和框架来处理 XML。在 Mule 中,您应该使用 DataWeave 转换。这个有点直观,因为它需要使用很少使用的名称空间选择器。选择器 returns 命名空间 URL,而不是名称。我创建了这个简单的函数来从输出中删除命名空间:

%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/")

在 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
    }
  }

这是一个使用名称空间前缀而不是 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"