XML Node Attribute Key is not honored

Hello @jan!

When converting XML to json with the XML node and setting Attribute key to let’s say $ the setting is not honored.

With this XML

<?xml version="1.0" encoding="utf-8"?>
<ORDERS05>
  <IDOC BEGIN="1">
    <EDI_DC40 SEGMENT="1">
      <TABNAM>EDI_DC40</TABNAM>
    </EDI_DC40>
  </IDOC>
</ORDERS05>

I would expetc the following JSON

[
  {
    "ORDERS05": {
      "IDOC": {
        "$BEGIN": "1",
        "EDI_DC40": {
          "$SEGMENT": "1",
          "TABNAM": "EDI_DC40"
        }
      }
    }
  }
]

But it returns this:

[
  {
    "ORDERS05": {
      "IDOC": {
        "BEGIN": "1",
        "EDI_DC40": {
          "SEGMENT": "1",
          "TABNAM": "EDI_DC40"
        }
      }
    }
  }
]

Or is this not the intended usage of this option? Is this purely for JSON-To-XML where $key would become an attribute?

Sample workflow:

{
  "name": "",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "xml",
              "value": "<?xml version=\"1.0\" encoding=\"utf-8\"?> <ORDERS05>   <IDOC BEGIN=\"1\">     <EDI_DC40 SEGMENT=\"1\">       <TABNAM>EDI_DC40</TABNAM>     </EDI_DC40>   </IDOC> </ORDERS05>"
            }
          ]
        }
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        170,
        1635
      ]
    },
    {
      "parameters": {
        "dataPropertyName": "xml",
        "options": {
          "explicitRoot": true
        }
      },
      "name": "XML",
      "type": "n8n-nodes-base.xml",
      "typeVersion": 1,
      "position": [
        320,
        1635
      ]
    }
  ],
  "connections": {
    "Set": {
      "main": [
        [
          {
            "node": "XML",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {}
}

Regads Sebastian

I just checked and it seems like it has to do with the fact that the node sets by default the option “mergeAttrs: true”. If you add that option manually and set it to false it works as expected.

Ok, I’ll try that on monday. The xml2js lib used is defaulting mergeAttrs to false. If n8n is reversing that it’s a possible solution

At least when I did try yesterday that solved the problem for me. So it should also work for you.

If it does not simply get back to me. Have a nice weekend!

Hey!

I tested it as you said and I am fine with that!

{
  "name": "XML Conversion",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "xml",
              "value": "<?xml version=\"1.0\" encoding=\"utf-8\"?> <ORDERS05>   <IDOC BEGIN=\"1\">     <EDI_DC40 SEGMENT=\"1\">       <TABNAM>EDI_DC40</TABNAM>     </EDI_DC40>   </IDOC> </ORDERS05>"
            }
          ]
        }
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        510,
        300
      ]
    },
    {
      "parameters": {
        "dataPropertyName": "xml",
        "options": {
          "attrkey": "$",
          "explicitRoot": true,
          "mergeAttrs": false
        }
      },
      "name": "XML",
      "type": "n8n-nodes-base.xml",
      "typeVersion": 1,
      "position": [
        740,
        300
      ]
    }
  ],
  "connections": {
    "Set": {
      "main": [
        [
          {
            "node": "XML",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "3"
}

produces

[
  {
    "ORDERS05": {
      "IDOC": {
        "$": {
          "BEGIN": "1"
        },
        "EDI_DC40": {
          "$": {
            "SEGMENT": "1"
          },
          "TABNAM": "EDI_DC40"
        }
      }
    }
  }
]

Thanks again!

@vuchl Great to hear! Have a nice evening!