ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Need help processing XML in luacode
@ 2020-03-10 19:13 Axel Kielhorn
  2020-03-10 19:53 ` mf
  0 siblings, 1 reply; 3+ messages in thread
From: Axel Kielhorn @ 2020-03-10 19:13 UTC (permalink / raw)
  To: ntg-context

[-- Attachment #1: Type: text/plain, Size: 1742 bytes --]

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



[-- Attachment #2: doclist.xml --]
[-- Type: application/xml, Size: 2416 bytes --]

[-- Attachment #3: prozess.xml --]
[-- Type: application/xml, Size: 7304 bytes --]

[-- Attachment #4: prozess-style-ecm.tex --]
[-- Type: application/octet-stream, Size: 2340 bytes --]

% !TEX TS-program = ConTeXt (LuaTeX 1.0.9)
% Copyright 2019 Axel Kielhorn
% Lizenz: CC-BY-SA 4.0 Unported http://creativecommons.org/licenses/by-sa/4.0/deed.de

% Usage:
% context --environment=prozess-style-ecm.tex prozess.xml

\startluacode
settings = {}
docstruktur = {}
doclistfile = "doclist.xml"

cropstring = function(string)
	return utilities.strings.striplines(string, "prune and collapse")
end

doc = xml.load(doclistfile, settings)

for v in xml.collected(doc,"/doclist/psdoc/") do
	print (v)
	print (xml.filter(v,"/docnr/text()"))
	docnr = (xml.filter(v,"/docnr/text()"))
	print (docnr)
	docname = (cropstring(xml.filter(v,"/docname/text()")))
	-- es kann mehrere DOCAN geben!
	docan = (cropstring(xml.filter(v,"/docan/text()")))
	docverantwortlich = (cropstring(xml.filter(v,"/docverantwortlich/text()")))
	docstruktur[docnr]={}
--	docstruktur[docnr]["docname"] = docname
--	docstruktur[docnr]["docan"] = docan
--	docstruktur[docnr]["docverantwortlich"] = docverantwortlich
end
\stopluacode

\language[de]
\setbreakpoints[compound]

\setuphyphenation[method=expanded]

\setuppapersize [A4,landscape][A4,landscape]
\setuplayout    [width=middle,  backspace=2in, cutspace=1in, leftmargin=1.5in,
                 height=middle, topspace=0.75in, bottomspace=0.75in]

\setuppagenumbering[location={footer,right}]

\setupheader[text][start]
\setupfooter[text][start]
%\setupheadertexts[][\date]

 \setupheadertexts[title][\date]
 \setuphead[title]
   [placehead=empty,
    before=,
    after=,
    page=,]

\startxmlsetups xml:prozessb
    \xmlsetsetup{#1}{*}{-}
    \xmlsetsetup{#1}{prozess|pschritt|psindoc|docan|psoutdoc|aschritt|eschritt}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:prozessb}

\startxmlsetups xml:prozess
  \title{\xmltext{#1}{/pnr} \xmltext{#1}{/pname}}
%  \setupheadertexts[\bf\xmltext{#1}{/pnr} \xmltext{#1}{/pname}][\date]
  {\bTABLE
    \setupTABLE[frame=off]
    \setupTABLE[offset=1mm]
    \setupTABLE[c][1][align=right, width=14cm]
    \setupTABLE[c][2][align=left,  width=6cm]
    \setupTABLE[c][3][align=right, width=4cm]
    \bTR \bTD \bfb\xmltext{#1}{/pnr}   \eTD \bTD Version:        \eTD \bTD \bf\xmltext{#1}{/pversion}\eTD \eTR
    \bTR \bTD \bfb\xmltext{#1}{/pname} \eTD \bTD Verantwortlich: \eTD \bTD \bf\xmltext{#1}{/pverantwortlich}\eTD\eTR
  \eTABLE}
  \blank
  \xmlflush{#1}
\stopxmlsetups

[-- Attachment #5: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Need help processing XML in luacode
  2020-03-10 19:13 Need help processing XML in luacode Axel Kielhorn
@ 2020-03-10 19:53 ` mf
  2020-03-11  8:33   ` Axel Kielhorn
  0 siblings, 1 reply; 3+ messages in thread
From: mf @ 2020-03-10 19:53 UTC (permalink / raw)
  To: ntg-context

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
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Need help processing XML in luacode
  2020-03-10 19:53 ` mf
@ 2020-03-11  8:33   ` Axel Kielhorn
  0 siblings, 0 replies; 3+ messages in thread
From: Axel Kielhorn @ 2020-03-11  8:33 UTC (permalink / raw)
  To: ntg-context

Hello Massi,

thanks for you help.


> Am 10.03.2020 um 20:53 schrieb mf <massifr@fastwebnet.it>:
> 
> 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

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-03-11  8:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-10 19:13 Need help processing XML in luacode Axel Kielhorn
2020-03-10 19:53 ` mf
2020-03-11  8:33   ` Axel Kielhorn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).