[NTG-context] Xml filtering in Lua

Hans Hagen j.hagen at xs4all.nl
Thu Nov 17 11:04:40 CET 2022


On 11/16/2022 8:56 PM, mf via ntg-context wrote:
> This works:
> 
> local metadata = xml.filter (t, '../../topics/topic[@t:id=="' .. ch_id 
> .. '"]')
> 
> also this:
> 
> local lpath = string.format('../../topics/topic[@t:id=="%s"]', ch_id)
> 
> local metadata = xml.filter (t, lpath)
> 
> 
> It looks like xml.filter supports only 2 arguments (see lxml-tex.lua), 
> and so it doesn't let you use string formatting patterns like the 
> "context" command does.
> 
> You can write:
> 
> context('the value of @t:id is "%s"', ch_id)
> 
> but you can't write:
> 
> xml.filter (t, '../../topics/topic[@t:id=="%s"]', ch_id)
In Thomas example this is also an approach:

\startxmlsetups xml:document
	\xmlfunction{#1} {document}
  	\xmlflush{#1}
\stopxmlsetups

with

\startluacode
local topics   = { }
local chapters = { }

function xml.functions.document(t)
     for c in xml.collected(t,"/topics/topic") do
         topics[c.at.t_id] = c
     end
  -- for c in xml.collected(t,"/chapters/chapter") do
  --     chapters[c.at.ch_id] = c
  --     -- or flush here
  -- end
end

function xml.functions.chapter (t)
     local ch_id = t.at.ch_id
     local metadata = topics[ch_id]
     lxml.command(t, ".", "xml:chapter:content")
     context.par ()
     context (ch_id)
     context.par ()
end
\stopluacode

so, basically you collect data and use it later ... for huge datasets 
that saves some time

if you have only chapters to process you can even decide to flush in 
that function

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------



More information about the ntg-context mailing list