Need help processing XML in luacode
Hi, after one year I’m back at a project processing XML with ConTeXt. Here is what I want to do: I have an XML file that I want to format with ConTeXt. I got this working last year. Now I want to take some information from a second file. My idea is to read the second file, store the data into a table and write auxiliary functions to access the data and insert them into the TeX output. When I process the included files with context --environment=prozess-style-ecm.tex prozess.xml I get [ctxlua]:19: table index is nil 12 13 doc = xml.load(doclistfile, settings) 14 15 for v in xml.collected(doc,"/doclist/psdoc/") do 16 -- print ((xml.filter(v,"//docnr/stripped()"))) 17 docnr = (cropstring(xml.filter(v,"/docnr/text()"))) 18 docname = (cropstring(xml.filter(v,"/docname/text()"))) 19 -- es kann mehrere DOCAN geben! 20 docan = (cropstring(xml.filter(v,"/docan/text()"))) 21 docverantwortlich = (cropstring(xml.filter(v,"/docverantwortlich/text()"))) 22 >> docstruktur[docnr]={} The xml.collected works, but the xml.filter returns nil. What did I miss? mtx-context | ConTeXt Process Management 1.03 mtx-context | mtx-context | main context file: /Volumes/Macintosh HD/usr/local/texlive/context-109/tex/texmf-context/tex/context/base/mkiv/context.mkiv mtx-context | current version: 2020.01.30 14:13 mtx-context | main context file: /Volumes/Macintosh HD/usr/local/texlive/context-109/tex/texmf-context/tex/context/base/mkiv/context.mkxl mtx-context | current version: 2020.01.30 14:13 The same code works when I use it in a Lua file that I call with mtxrun --script panalyse.lua Greetings Axel
In your tex file, try this: \startluacode settings = {} docstruktur = {} doclistfile = "doclist.xml" local striplines = utilities.strings.striplines local xmltext = xml.text cropstring = function(s) -- return striplines(s, "prune and collapse") return striplines(s) end doc = xml.load(doclistfile, settings) for v in xml.collected(doc,"/doclist/psdoc/") do -- print (v) -- print (xml.text(v,"/docnr")) local docnr = cropstring(xml.text(v,"/docnr")) print (docnr) local docname = cropstring(xmltext(v,"/docname")) -- es kann mehrere DOCAN geben! local docan = (cropstring(xmltext(v,"/docan"))) local docverantwortlich = (cropstring(xmltext(v,"/docverantwortlich"))) docstruktur[docnr]={ docname = docname, docan = docan, docverantwortlich = docverantwortlich } -- docstruktur[docnr]["docname"] = docname -- docstruktur[docnr]["docan"] = docan -- docstruktur[docnr]["docverantwortlich"] = docverantwortlich end \stopluacode hoping it helps, Massi
Hello Massi, thanks for you help.
Am 10.03.2020 um 20:53 schrieb mf
: In your tex file, try this:
\startluacode settings = {} docstruktur = {} doclistfile = "doclist.xml"
local striplines = utilities.strings.striplines
cropstring = function(s) -- return striplines(s, "prune and collapse") return striplines(s) end
Well, with this definition I can actually inline that.
local xmltext = xml.text
doc = xml.load(doclistfile, settings)
for v in xml.collected(doc,"/doclist/psdoc/") do -- print (v) -- print (xml.text(v,"/docnr")) local docnr = cropstring(xml.text(v,"/docnr")) print (docnr) local docname = cropstring(xmltext(v,"/docname")) -- es kann mehrere DOCAN geben! local docan = (cropstring(xmltext(v,"/docan"))) local docverantwortlich = (cropstring(xmltext(v,"/docverantwortlich")))
docstruktur[docnr]={ docname = docname, docan = docan, docverantwortlich = docverantwortlich }
Great, that is even shorter.
end \stopluacode
I’m cleaning up my backend code to make it more Lua like. Greetings Axel
participants (2)
-
Axel Kielhorn
-
mf