[NTG-context] Xml filtering in Lua

Thomas A. Schmitz thomas.schmitz at uni-bonn.de
Wed Nov 16 17:10:19 CET 2022

Hi all,

I thought this was easy, but I overestimated my competence… I want to filter xml elements via their attributes and retrieve and typeset parts belonging together. Here is a small test file that explains what I’m trying:

      <topic id="test1">
	<title>This is the first test</title>
      <topic id="test2">
	<title>This is the second test</title>
      <chapter id="test1">
	  This will be the content of the <emph>first</emph> chapter.
      <chapter id="test2">
	  This will be the content of the <emph>second</emph> chapter.

\startxmlsetups xml:testsetups


\startxmlsetups xml:document

\startxmlsetups xml:chapters

\startxmlsetups xml:chapter
	\xmlfunction {#1} {chapter}
	 \xmlflush {#1}	

\startxmlsetups xml:chapter:content
	\xmltext {#1} {content}

\startxmlsetups xml:emph
   {\em \xmlflush {#1}}

function xml.functions.chapter (t)
	 local ch_id = t.at.id
		local metadata = xml.filter (root, '../../topics/topic[@id=="%s"]', ch_id)
		print (inspect(metadata))
		lxml.command(t, ".", "xml:chapter:content")
		context.par ()
		context (ch_id)
		context.par ()


The line with xml.filter does not work as I expected. How can I walk the tree, find the “topic” element with the same “id” attribute as the chapter I’m currently in, and then typeset the different children of the topic element? 

Thanks a lot and all best


More information about the ntg-context mailing list