'mule 3.9.5 Cannot transform xml dom to xml
After migration the mule server from 3.9.1 to 3.9.5, I encountered a problem in the transformation of the payload to xml.
Here is my code:
<flow name="SetVariablesFromPBGB">
<foreach collection="#[xpath3('/*:Envelope/*:Header',payload,'NODESET')]" doc:name="For Each">
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<mulexml:xslt-transformer xsl-file="removeNameSpace.xslt" maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT"/>:
<set-variable variableName="soapHeader" value="#[System.getProperty('line.separator')]#[message.payloadAs(java.lang.String)]"/>
</foreach>
</flow>
The dom-to-xml-transformer don't transform the payload to xml:
After this line the payload still [soapenv:Header: null] instead of :
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Header>
</soapenv:Header>
Solution 1:[1]
My understanding is that you want to get the soapEnv:Header element with the prefix soapEnv: but without the namespace declaration for it. I'm not sure that is valid usage of namespaces according to the XML Namespaces specification:
The namespace prefix, unless it is xml or xmlns, MUST have been declared in a namespace declaration attribute in either the start-tag of the element where the prefix is used or in an ancestor element (i.e., an element in whose content the prefixed markup occurs).
If having the namespace declared is acceptable this DataWeave script could replace the flow's body:
<dw:transform-message doc:name="Transform Message">
<dw:input-payload mimeType="application/xml" />
<dw:set-payload><![CDATA[%dw 1.0
%dw 1.0
%output application/xml
---
payload.Envelope.*Header
]]>
</dw:set-payload>
</dw:transform-message>
Input:
<soapenv:Envelope xmlns:soapenv="schemas.xmlsoap.org/soap/envelope"> <soapenv:Header><t>a</t></soapenv:Header> <soapenv:Body> <import> <data><![CDATA[ <AAA> <BBB></BBB> </AAA>]]></data> </import> </soapenv:Body> </soapenv:Envelope>
Output:
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Header xmlns:soapenv="schemas.xmlsoap.org/soap/envelope">
<t>a</t>
</soapenv:Header>
If you want to remove completely the XML namespaces you can use the method adapted from this blog by using a recursive function. This removes completely namespaces declarations and the prefixes.
%dw 1.0
%output application/xml
---
Header: payload.Envelope.Header
Output (for the same input above):
<?xml version='1.0' encoding='UTF-8'?>
<Header>
<t>a</t>
</Header>
Those are the valid XML outputs that you can perform.
NOT RECOMMENDED: If you absolutely must generate invalid XML then you can convert the XML to a string and perform string replacement or regex operation to remove the undesired. Be aware that performing string manipulations on XML is a bad practice. I strongly advise to avoid implementing this and to avoid generating invalid XML.
Example:
%dw 1.0
%output application/java
---
// warning: transforming the XML with string operations is not recommended!
write(payload.Envelope.*Header,"application/xml")
This returns a Java string that can be manipulated inside DataWeave, Java code, MEL scripts or Groovy scripts.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
