javascriptmirth

Delete XML Atrributes


I am trying to write up some code in Mirth Connect that takes an XML tag and removes all attributes from inside of it.

Example:

<code nullFlavor="UNK" xsi:type="CD">

Desired result:

<code/>

I could do something like this:

delete msg['code']['@nullFlavor']
delete msg['code']['@xsi:type']

But I ideally want to handle ALL possible values that could come in, not just nullFlavor and xsi:type. Is there a way to do this?


Solution

  • It can be achieved by running the following script-

    // Get the <code> element
    var myElement = msg['code'];
    
    // Check if the element exists
    if (myElement) {
        // Loop through all attributes and remove them
        for each (var attr in myElement.@*) {
            delete myElement['@' + attr.name()];
        }
    }
    
    // Return the modified message
    msg;