0

I am trying to create below XML in mulesoft 4.4.0 using dataweave script.

<?xml version='1.0' encoding='UTF-8'?>
<batch>
  <nsA:parent xmlns:nsA="http://www.testA.com/core" xmlns:nsB="http://www.testB.com/core">
    <nsB:child >ChildNodeValue</nsB:child>
  </nsA:parent>
</batch>

Dataweave scripts I tried are :

1st Try:

%dw 2.0
output application/xml
ns nsA http://www.testA.com/core
ns nsB http://www.testB.com/core

---
batch: {
    (payload map ((item, index) -> {
        nsA#parent:{
            nsB#child:"ChildNodeValue"
        }
    })
 )}


-------------------------------------

Output:

<?xml version='1.0' encoding='UTF-8'?>
<batch>
  <nsA:parent xmlns:nsA="http://www.testA.com/core">
    <nsB:child xmlns:nsB="http://www.testB.com/core">ChildNodeValue</nsB:child>
  </nsA:parent>
</batch>

2nd try: : Using writeDeclaredNamespaces="All"

%dw 2.0
output application/xml writeDeclaredNamespaces="All"
ns nsA http://www.testA.com/core
ns nsB http://www.testB.com/core

---
batch: {
    (payload map ((item, index) -> {
        nsA#parent:{
            nsB#child:"ChildNodeValue"
        }
    })
 )}


---------------------------
Output:

<?xml version='1.0' encoding='UTF-8'?>
<batch xmlns:nsA="http://www.testA.com/core" xmlns:nsB="http://www.testB.com/core">
  <nsA:parent>
    <nsB:child>ChildNodeValue</nsB:child>
  </nsA:parent>
</batch>

How can I move the namespaces to a specific parent node (not the root node) in XML output?

Thank you

4
  • By default DataWeave will emit the namespaces in the first node that use them, which is compatible with XML standards. Why do you need to select a specific node? Commented Jan 4, 2024 at 23:52
  • Thanks, The requirement is because of my downstream system (which is not in my control) takes xml in this format only. Commented Jan 5, 2024 at 9:58
  • 1
    Then be aware that the downstream system is not fully compliant with the XML Schema standard. Commented Jan 5, 2024 at 11:22
  • @aled Thanks, Yes, That makes sense. I will check with them. Commented Jan 8, 2024 at 18:15

1 Answer 1

2

One way is to define the namespaces as attributes like below.

%dw 2.0
output application/xml
---
batch: {
    "nsA:parent" @(
        "xmlns:nsA": "http://www.testA.com/core",
        "xmlns:nsB": "http://www.testB.com/core"
    ):{
        "nsB:child":"ChildNodeValue"
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, adding namespace as attributes and writing nides in double quotes solved the issue
This works for me too, but I use a different syntax for the namespace. I add the namespace declaration in the header, and I use nsA#parent without quotes for the element name. This still needs the xmlns attributes to be added, but the value can use the namespace reference from the header

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.