muledataweavemule-studiomule4mule-esb

XML attributes being lost after adding namespace prefix to an existing XML


I need to add namespace and prefix(ns0) to the root tag of an existing XML. I tried doing it with the piece of code below. However the output has removed all the existing attributes that were already in the XML root tag. Please see the sample input and expected output below. Thanks in advance!

%dw 2.0
output application/xml writeDeclaredNamespaces="All"  
ns ns0 http://Test.Sample.data/1.0
---
ns0#trans: payload.trans

Original XML:

<?xml version='1.0' encoding='UTF-8'?>
<trans xmlns:ns0="http://Test.Sample.data/1.0" id="1495144" TPId='4aec' change='0' count='1' dateStamp="2024-08-02T03:07:48" effectiveDate="2024-08-01"transId="12447055">
    <data id="FF2AD08D621">
        <StateFlag>0</StateFlag>
        <DCC>B</DCC>
        <LastName>MMNGorthy</LastName>
        <Name>Scott</Name>
        <Structure>NonProfit</Structure>
        <BusinessType>Engineering</BusinessType>
        <meams>AI</meams>
        <Count>0</Count>
        <CTier>000</CTier>
        <CTCond>111</CTCond>
        <CTPl>222</CTPl>
        <DO>1</DO>
        <AuthObject>0</AuthObject>
    </data>
</trans>

Expected output(Please note the ns0 prefix):

<?xml version='1.0' encoding='UTF-8'?>
<ns0:trans xmlns:ns0="http://Test.Sample.data/1.0" id="1495144" TPId='4aec' change='0' count='1' dateStamp="2024-08-02T03:07:48" effectiveDate="2024-08-01"transId="12447055">
    <data id="FF2AD08D621">
        <StateFlag>0</StateFlag>
        <DCC>B</DCC>
        <LastName>MMNGorthy</LastName>
        <Name>Scott</Name>
        <Structure>NonProfit</Structure>
        <BusinessType>Engineering</BusinessType>
        <meams>AI</meams>
        <Count>0</Count>
        <CTier>000</CTier>
        <CTCond>111</CTCond>
        <CTPl>222</CTPl>
        <DO>1</DO>
        <AuthObject>0</AuthObject>
    </data>
</ns0:trans>

Solution

  • Since you are creating a new top element in the transformation you need to 'copy' the attributes yourself to the output as described in the documentation page Pass XML Attributes from the input element payload.trans :

    %dw 2.0
    output application/xml writeDeclaredNamespaces="All"  
    ns ns0 http://Test.Sample.data/1.0
    ---
    ns0#trans @((payload.trans.@)): payload.trans
    

    Output:

    <?xml version='1.0' encoding='UTF-8'?>
    <ns0:trans xmlns:ns0="http://Test.Sample.data/1.0" id="1495144" TPId="4aec" change="0" count="1" dateStamp="2024-08-02T03:07:48" effectiveDate="2024-08-01" transId="12447055">
      <data id="FF2AD08D621">
        <StateFlag>0</StateFlag>
        <DCC>B</DCC>
        <LastName>MMNGorthy</LastName>
        <Name>Scott</Name>
        <Structure>NonProfit</Structure>
        <BusinessType>Engineering</BusinessType>
        <meams>AI</meams>
        <Count>0</Count>
        <CTier>000</CTier>
        <CTCond>111</CTCond>
        <CTPl>222</CTPl>
        <DO>1</DO>
        <AuthObject>0</AuthObject>
      </data>
    </ns0:trans>