manoelcampos / xml2lua

XML Parser written entirely in Lua that works for Lua 5.1+. Convert XML to and from Lua Tables 🌖💱
MIT License
290 stars 74 forks source link

Calling xml2lua.toXml removes attributes #84

Closed ArthurAttout closed 1 year ago

ArthurAttout commented 2 years ago

Calling xml2lua.toXml() twice on the same structure yields the same result, once with attributes and once without.

AFAIK, this call should be idempotent, I don't see any reason why it would return two distinct results.

Sample code :

local xml2lua = require("xml2lua")
local people = {
    person = {
        {name="Manoel", city="Palmas-TO", _attr={ type='natural' } },
        {name="Breno", city="Palmas-TO", _attr={ type='legal' } }
    }
}

print("People Table\n")
xml2lua.printable(people)

print()
print("XML Representation\n")
print(xml2lua.toXml(people, "people"))
print(xml2lua.toXml(people, "people"))

Result :

People Table

  person
    1
      _attr
        type=natural
      name=Manoel
      city=Palmas-TO
    2
      _attr
        type=legal
      name=Breno
      city=Palmas-TO

XML Representation

<people>
    <person type="natural">
      <name>Manoel</name>
      <city>Palmas-TO</city>
    </person>
    <person type="legal">
      <name>Breno</name>
      <city>Palmas-TO</city>
    </person>
</people>

<people>
    <person>
      <name>Manoel</name>
      <city>Palmas-TO</city>
    </person>
    <person>
      <name>Breno</name>
      <city>Palmas-TO</city>
    </person>
</people>

Tested on 1f8d90b.

nilzao commented 1 year ago

PR #89 fixes it