JSON To XML

Hello n8n experts,

I’m trying to generate an XML form a JSON. I’m struggling to add the attributes of the header tag. I’m using the XML node with the option “Attribute Key” with a $.

When I add a the attribute as a field starting with $ I get the error: “Invalid character in name”.

What would be the right way of doing it?

Below you can see a workflow that shows the issue:

Information on our n8n setup

  • n8n version: 1.21.0
  • Database (default: SQLite): postgresql 14.8
  • n8n EXECUTIONS_PROCESS setting (default: own, main): queue
  • Running n8n via (Docker, npm, n8n cloud, desktop app): kubernetes
  • Operating system: amazon linuex

Hey @Fortian,

I don’t think XML allows a $ at the start of an attribute name which could be why you are seeing an issue.

I found hte XML spec on nameStartChars here which could be useful: Extensible Markup Language (XML) 1.0 (Fifth Edition)

I could also be reading it wrong but it looks to be fairly strict on first characters.

Hello @Jon

I don’t want the final XML to have any $. But I do want the xml nodes to have some attributes and I understood the attribute key was just a way to identify the attributes from the input.

Do you have an example of an XML node generating an XML with notes with attributes?

Hey @Fortian,

If you take your example and remove the $ from the input json it will generate the example. The issue is becuase it is there we are trying to use it in the output, If you don’t want the $ maybe renaming the key to remove the $ would work before converting to XML.

Hello @Jon ,

Thanks for your answer but I think I didn’t explain properly what I’m trying to achieve.

Let’s say I want to generate an XML like the following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order>
    <thing>
        <element key="1">
            <id_art>00061</id_art>
            <lin>1</lin>
        </element>
        <element key="1">
            <id_art>01540</id_art>
            <lin>2</lin>
        </element>
    </thing>
</order>

How do I add the attribute “key” to the element tag (and how do I ensure elements are nested inside the thing tag.)?

Hey @Fortian,

That sounds like a different question but I understand it, I will need to have a play with the node and check the docs for the package we use to see if that is possible.

Hi there,

I was also messing around with it a couple of months ago. If I recall correctly you create a key $ with an object as value where you put the attributes.
So something like:
Element: {
$:{
Key:“some id”
}
ElementField:“value”
}

@Fortian , here’s your solution (@BramKn was right):

Note the changes to structure of the JSON and how the attribute you need resides as the nested property of element:

{
  "thing": [
    {
      "element": {
        "id_art": "00061",
        "lin": 1,
        "$": {
          "key": 1
        }
      }
    },
    {
      "element": {
        "id_art": "01540",
        "lin": 2,
        "$": {
          "key": 1
        }
      }
    }
  ]
}

The XML output as a result is

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order>
  <thing>
    <element key="1">
      <id_art>00061</id_art>
      <lin>1</lin>
    </element>
  </thing>
  <thing>
    <element key="1">
      <id_art>01540</id_art>
      <lin>2</lin>
    </element>
  </thing>
</order>

Thank you all!
I think it’s a pity that n8n documentation doesn’t properly describe this feature :smiley: