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

xml2lua.toXml() : Duplicated XML tags is broken when converting from Lua to XML #87

Open chrisaga opened 1 year ago

chrisaga commented 1 year ago

Use case : create a lua table and convert it to XML with duplicated tags

based on xml2lua/example3.lua we could expect :

local tag = { tag1={ 1='A', 2='B' } }
print(xml2lua.toXml(tag, 'tag')

would give :

<tag>
    <tag1>A</tag1>
    <tag1>B</tag1>
</tag>

but it gives insead :

<tag>
    <tag1>
      <tag1>A</tag1>
      <tag1>B</tag1>
    </tag1>
</tag>

The easyest way to test it is to append the following line at the end of xml2lua/example3.lua (select the 3rd test by uncommenting line 18) :

print(xml2lua.toXml(handler.root.tag, 'tag'))
nilzao commented 1 year ago

I couldn't reproduce the behavior.

Use this lua table to achieve the xml result.

local xml2lua = require("xml2lua")
local tagtable = { tag = { tag1 = { [1] = 'A', [2] = 'B' } } }
print(xml2lua.toXml(tagtable))
<tag>
    <tag1>A</tag1>
    <tag1>B</tag1>
</tag>
mbabloapifonica commented 1 year ago

I have a similar bug:

local initstr = [[<grammar xmlns="http://www.w3.org/2001/06/grammar" xml:lang="pl-PL" version="1.0" root="transcribe">
    <meta name="model" content="default"/>
    <tag name="language000" content="pl-PL"/>
    <rule id="transcribe">
        <one-of>
            <item>item1</item>
            <item>item2</item>
        </one-of>
    </rule>
</grammar>]]

xmlParser:parse(initstr)
print(initstr)
print("===")
print(xml2lua.toXml(xmlHandler.root))

so I try to load xml and the convert it back to xml, the response I get is:

<grammar xmlns="http://www.w3.org/2001/06/grammar" xml:lang="pl-PL" version="1.0" root="transcribe">
    <meta name="model" content="default"/>
    <tag name="language000" content="pl-PL"/>
    <rule id="transcribe">
        <one-of>
            <item>item1</item>
            <item>item2</item>
        </one-of>
    </rule>
</grammar>
===
<grammar root="transcribe" xmlns="http://www.w3.org/2001/06/grammar" version="1.0" xml:lang="pl-PL">
        <rule id="transcribe">
            <one-of>
                <item>
                  <item>item1</item>
                  <item>item2</item>
                </item>
            </one-of>
        </rule>
          <meta content="default" name="model">

          </meta>
            <tag content="pl-PL" name="language000">

            </tag>
    </grammar>

as you see one-of part looks in a different way:

            <one-of>
                <item>
                  <item>item1</item>
                  <item>item2</item>
                </item>
            </one-of>

instead of this

        <one-of>
            <item>item1</item>
            <item>item2</item>
        </one-of>
mbabloapifonica commented 1 year ago

UPD: I found out this problem happening on v.1.5-2 (the latest version that is currently available on luarocks) Everything is working correctly on v.1.5-1 version. I'm not sure about v.1.6-1 as it is not available yet (for today) in luarocks

Crazyokd commented 1 month ago

@mbabloapifonica image