2

I am trying implement the default namespace while creating XML in the data weaver. I went thought below data weaver documentation and earlier posts. Could not get any clue on this. How can we declare default namespace.

https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-tutorial

My input will be from DB - linked list. Output is XML.

%dw 1.0
%namespace ns0 urn:abc:def:Components
%output application/xml
---
{
    ns0#University: {
        college: payload.college[0],
        (payload map ((payload01 , indexOfPayload01) -> {
            dept: {
                deptName: payload01.dept,
                Noofstudents: payload01."No of students" as :number
            }
        }))
    }
}

Current output is

<ns0:University xmlns:ns0="urn:abc:def:Components">
<college>abc</college> <!-- this is simple tag/ It will not repeat -->
<dept> <!-- This is complex tag -->
   <dept Name>IT</dept Name>
   <No of students>5</No of students>
</dept>
<dept>
   <dept Name>IT</dept Name>
   <No of students>5</No of students>
</dept>
<dept>
   <dept Name>IT</dept Name>
   <No of students>5</No of students>
</dept>
</ns0:University>

Expected output is

<University xmlns="urn:abc:def:Components">
<college>abc</college> <!-- this is simple tag/ It will not repeat -->
<dept> <!-- This is complex tag -->
   <dept Name>IT</dept Name>
   <No of students>5</No of students>
</dept>
<dept>
   <dept Name>IT</dept Name>
   <No of students>5</No of students>
</dept>
<dept>
   <dept Name>IT</dept Name>
   <No of students>5</No of students>
</dept>
<University>
Simbu
  • 766
  • 1
  • 12
  • 37

2 Answers2

1

For the solution of above problem you can add the xmlns="urn:abc:def:Components" as attribute to the 'University' tag. Do not use the namespaces logic here otherwise it will create University tag with ns0# namespace.

Like as University @(xmlns:"urn:abc:def:Components")

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
rangrao
  • 11
  • 2
0

I could not find solution to implement default namespace in request xml construction through DataWeave.

Though i can use below sort of Dataweave as temporary solution. Appended the namespace "ns0#" in all the request xml tags.

Kindly let me know, if there is any simplest way to do.

%dw 1.0
%namespace ns0 urn:abc:def:Components
%output application/xml
---
{
    ns0#University: {
        ns0#college: payload.college[0],
        (payload map ((payload01 , indexOfPayload01) -> {
            ns0#dept: {
                ns0#deptName: payload01.dept,
                ns0#Noofstudents: payload01."No of students" as :number
            }
        }))
    }
}
Simbu
  • 766
  • 1
  • 12
  • 37