0

Need to remove namespace from XML by keeping everything else as is including the attributes. I tried using replace and update but it does not seem to work. Below is the sample XML where I need to remove the namespace (ds2).

Input XML:

<ds2:note xmlns:ds2="http://sampleSchame"  code= "12332" Id="333-333-333" set="2" St"2024-09-11T11:23:55" sq="2">
<customer>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</customer>
</ds2:note>

Expected Output:

<note xmlns:ds2="http://sampleSchame"  code= "12332" Id="333-333-333" set="2" St"2024-09-11T11:23:55" sq="2">
<customer>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</customer>
</note>
4
  • The expected output still has the namespace. Seems to be a copy of the input. Commented Sep 19, 2024 at 21:04
  • if you notice the ds2 prefix has been removed from the starting and ending tag in the expected output. I kept the attribute (xmlns:ds2="sampleSchame") in the output because that's how they wanted Commented Sep 20, 2024 at 13:50
  • Why the namespace definition is required if the namespace is not used? Doesn't seem to make sense. Commented Sep 20, 2024 at 14:24
  • "but it does not seem to work" What do you mean? What happens, exactly? Commented Sep 20, 2024 at 21:36

1 Answer 1

0

You could use the function from answer Error trying to remove a namespace from an XML in Mule 4 to remove the usage of a namespace and then adding a 'fake' definition simulating it is an attribute.

Example:

%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://sampleSchame")
     mapObject ((value, key) -> 
        (key) @("xmlns:ds2":"http://sampleSchame", (key.@)): value
    )

This script assumes that there is only one element -which should be the case for XML payloads- and adds the namespace definition as an attribute.

It is not clear why it would be needed since it is not used. Seems to indicate bad assumptions or parser on the consumer of this output.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.