speedata / publisher

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

`decode_xml()` created table name? #480

Closed pr-apes closed 1 year ago

pr-apes commented 1 year ago

@pgundlach,

I read in the documentation about decode_xml():

Reads an XML file and creates a table in the format from encode_table. Return value 1 is a bool (success), value 2 is the error message if the first value is false or the table if the first value is true.

Is there any way to influence the table name? At least, which is the name of the imported table?

I'm afraid imported XML data are of no use in the filter, unless I can access them.

Sorry, but I guess either the documentation or myself lacks something.

Many thanks for your help.

pgundlach commented 1 year ago

Does this help?

local xml = require("xml")
dofile("debug.lua")

ok ,tab = xml.decode_xml("data.xml")
printtable("data",tab)

data.xml:

<root>
    <child>1</child>
    <child>2</child>
    <child><abc>abc</abc></child>
</root>

lua table:

data = {
  [1] = "\n    "
  [2] = {
    [1] = "1"
    ["_type"] = "element"
    ["_name"] = "child"
  },
  [3] = "\n    "
  [4] = {
    [1] = "2"
    ["_type"] = "element"
    ["_name"] = "child"
  },
  [5] = "\n    "
  [6] = {
    [1] = {
      [1] = "abc"
      ["_type"] = "element"
      ["_name"] = "abc"
    },
     ["_type"] = "element"
     ["_name"] = "child"
    },
    [7] = "\n"
    ["_type"] = "element"
    ["_name"] = "root"
 }

(formatting is a bit broken)

pgundlach commented 1 year ago

the table above is the second argument of xml.decode_xml()

pr-apes commented 1 year ago

Sorry for the delayed reply, @pgundlach.

I have to check this later, but I have a comment.

I cannot use dofile("debug.lua") unless full path is specified. I wonder whether this is intended.

Many thanks for your help.

pgundlach commented 1 year ago

This works for me where debug.lua is in the current directory.

I will close this as fixed. If there are still problems, please re-open.

pr-apes commented 1 year ago

It works for me with current version (4.13.12) as require("debug").

Is this the standard way of invoking it?

pgundlach commented 1 year ago

Actually I am not a Lua expert regarding dofile() and require(). I use dofile("debug.lua") for my debugging script, but require could also work.

pr-apes commented 1 year ago

In my case, require() seems to find the file in the Publisher tree.

dofile() requires full path and extension to find the file.

pr-apes commented 1 year ago

From Programming in Lua (first edition), section 8.1 – The require Function (https://www.lua.org/pil/8.1.html):

Lua offers a higher-level function to load and run libraries, called require. Roughly, require does the same job as dofile, but with two important differences. First, require searches for the file in a path; second, require controls whether a file has already been run to avoid duplicating the work. Because of these features, require is the preferred function in Lua for loading libraries.