Search code examples
muledataweavemulesoft

How to generate the custom xml headers?


I'm looking to fix an issue in my script which is not generating the expected xml headers and closing tag as expected. See the shared below details. Input Payload:

{ 
    "result": {
        "data": [
            [
                "jane",
                "ALDANA",
                "21"
            ],
            [
                "Emma",
                "EDWARDS",
                "23"
            ]
        ]
    }
}

i'm using this script:

%dw 2.0
output application/xml encoding="UTF-8", writeDeclaration=true


fun convertJsonToXml(json: Any): Any =
    if (json is Array)
        json map convertJsonToXml($)
    else if (json is Object)
        {
            (json mapObject {
                ($$): convertJsonToXml($)
            })
        }
    else
        json

---

{
    'Input:xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="csm_admin.xsd"':  {

        add: (convertJsonToXml(payload.result.data) map (item, index) -> {
            data: {
                "@firstName": item[0],
                lastName: item[1],
                age: item[2],
                
            }
        })
    }
}

resulted Output based on the above script:

<?xml version='1.0' encoding='UTF-8'?>
<Input:xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="csm_admin.xsd">
  <add>
    <data>
      <@firstName>jane</@firstName>
      <lastName>ALDANA</lastName>
      <age>21</age>
    </data>
  </add>
  <add>
    <data>
      <@firstName>Emma</@firstName>
      <lastName>EDWARDS</lastName>
      <age>23</age>
    </data>
  </add>
</Input:xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="csm_admin.xsd">

Expected xml output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<admin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="csm_admin.xsd">
  <add>
    <data>
      <@firstName>jane</@firstName>
      <lastName>ALDANA</lastName>
      <age>21</age>
    </data>
  </add>
  <add>
    <data>
      <@firstName>Emma</@firstName>
      <lastName>EDWARDS</lastName>
      <age>23</age>
    </data>
  </add>
</Input>

as explained above the issue which i'm still facing is the headers and the closing tag which need to be fixed. i tried different approaches but could not resolve the issue. Thanks for your inpt


Solution

  • Hi here is a code that produces exactly what you are expecting with a very simple script

    %dw 2.0
    output application/xml
    ---
    "input" @("xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation":"csm_admin.xsd"): {
        "add": {
            (
                payload.result.data map ((data, index) -> 
                    data: {
                        "@firstName": data[0],
                        "lastName": data[1],
                        "age": data[2]
                    }
                )
            )
        }
    }
    

    The way to add those attributes like "xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance" under @