speedata / publisher

speedata Publisher - a professional database Publishing system
https://www.speedata.de/
GNU Affero General Public License v3.0
293 stars 36 forks source link

avoid `xml.encode_table()` error? #477

Closed pr-apes closed 1 year ago

pr-apes commented 1 year ago

@pgundlach,

I have the following contents in a CSV file:

"a",
,"b"
"c",

I have the following filter:

local runtime = require("runtime")
local csv = require("csv")
local xml = require("xml")

local csvtab, msg = csv.decode(runtime.variables["csv_file"])

if not csvtab then
    print(msg)
    os.exit(-1)
else
  local ok, err = xml.encode_table(csvtab)
  if not ok then
    os.exit(-1)
  end
end

Invoking sp --dummy --filter=filter.lua -v csv_file=file.csv, I get the following error message and no conversion:

xml: start tag with no name
xml: start tag with no name

I guess empty cells are the root of the problem.

Would it be possible that empty cells won't prevent table export to XML file?

Many thanks for your help.

pgundlach commented 1 year ago

What would the XML file look like from the CSV table?

The CSV is like this:

{
  [1] = {
    [1] = "a"
    [2] = ""
  },
    [2] = {
      [1] = ""
      [2] = "b"
    },
      [3] = {
        [1] = "c"
        [2] = ""
      },
      },

I can't see what XML structure this could be. Perhaps I misunderstand the problem.

pr-apes commented 1 year ago

Sorry for the noise, @pgundlach.

I totally forgot that I had to name every element in the resulting XML file, so that the filter should read:

local runtime = require("runtime")
local csv = require("csv")
local xml = require("xml")

local csvtab, msg = csv.decode(runtime.variables["csv_file"])

if not csvtab then
    print(msg)
    os.exit(-1)
else
  csvtab._name = "data"

  for i=1,#csvtab do
    csvtab[i]._name = "row"
    for j=1,#csvtab[i] do
      val = csvtab[i][j]
      csvtab[i][j] = { _name = "cell", val }
    end
  end

  local ok, err = xml.encode_table(csvtab)
  if not ok then
    os.exit(-1)
  end
end

Sorry for the noise again. Closing the issue myself.

pgundlach commented 1 year ago

No problem.

I was thinking about automatically adding _name="cell" etc. to the table so that it can be exported like in your example, but that would require a different table layout. So I think the intermediate step to create a new table is necessary.

pr-apes commented 1 year ago

Many thanks for the explanation about the requirement for the new table.