Ask Your Question
3

How can XML tags be removed from the payload in Mule 3.9 while keeping the namespaces and attributes intact?

asked 2023-05-18 06:56:48 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-18 07:04:02 +0000

plato gravatar image

To remove XML tags from the payload in Mule 3.9 while retaining the namespaces and attributes, you can use the MEL (Mule Expression Language) expressions with the transformers.

Here is an example of how to do it:

  1. Use the XPath expression in MEL to select the contents of the element you want to remove.

#[xpath('//elementName/*')]

This expression selects all the child elements of the element named "elementName."

  1. Use the "groovy:XmlUtil.serialize" function to convert the result of the XPath expression back into XML with just the namespaces and attributes.

#[groovy:com.sun.org.apache.xml.internal.serialize.XmlSerializer.serialize(muleContext.transformMessage(payload, 'xpath://elementName/*'))]

The "transformMessage" function takes the generated XML as input and removes any unnecessary tags while retaining the namespaces and attributes.

Full code example:

<flow name="removeXmlTagsFlow">
  <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
  <set-payload value="#[xpath('//elementName/*')]" doc:name="Set Payload"/>
  <set-payload value="#[groovy:com.sun.org.apache.xml.internal.serialize.XmlSerializer.serialize(muleContext.transformMessage(payload, 'xpath://elementName/*'))]" doc:name="Set Payload"/>
  <!-- Other code goes here -->
</flow>

This code will remove the tags from the "elementName" element, keeping the namespaces and attributes intact.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-18 06:56:48 +0000

Seen: 7 times

Last updated: May 18 '23