ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Axel Kielhorn <tex@axelkielhorn.de>
To: "ntg-context@ntg.nl" <ntg-context@ntg.nl>
Subject: Need help processing XML in luacode
Date: Tue, 10 Mar 2020 20:13:37 +0100	[thread overview]
Message-ID: <916140D5-73D7-4874-8078-86337DF85927@axelkielhorn.de> (raw)

[-- 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
___________________________________________________________________________________

             reply	other threads:[~2020-03-10 19:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-10 19:13 Axel Kielhorn [this message]
2020-03-10 19:53 ` mf
2020-03-11  8:33   ` Axel Kielhorn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=916140D5-73D7-4874-8078-86337DF85927@axelkielhorn.de \
    --to=tex@axelkielhorn.de \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).