[NTG-context] processing xml with lua

Thomas A. Schmitz thomas.schmitz at uni-bonn.de
Tue Mar 5 09:47:48 CET 2013


Hi,

may I ask another question about my new favorite topic? No? Anyway: here 
comes. While processing xml, I would like to store the content of a node 
in a lua table and retrieve it later. The example is silly, but 
demonstrates my problem. Is there a way to have ConTeXt process and 
typeset the value in lines 3 and 4? As you can see, it typesets the raw 
xml instead of processing it. (I suspect I'm still a bit lost regarding 
the difference between xml... and lxml... functions, but I couldn't hit 
the right combination here).

Thanks, and all best

Thomas

\startbuffer[test]
<a>
   <b><c>Fruit</c> <d><e>Apple</e></d></b>
   <b><c>Vegetable</c> <d><q>Carot</q></d></b>
   <b><c>Fruit</c></b>
   <b><c>Vegetable</c></b>
</a>
\stopbuffer

\startxmlsetups xml:testsetups
	\xmlsetsetup{\xmldocument}{a|b|c|d}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:testsetups}

\startxmlsetups xml:a
	\xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:b
	\xmlfunction{#1}{lookup} \par
\stopxmlsetups

\startxmlsetups xml:e
	{\bgroup\em \xmlflush{#1}\egroup}
\stopxmlsetups

\startxmlsetups xml:q
	\quotation{\xmlflush{#1}}
\stopxmlsetups

\startluacode
lookuptable = { }
function xml.functions.lookup(t)
   mytype = xml.text(t, "c")
   myvalue = xml.text(t, "d")
   if not lookuptable[mytype] then
     context("New Type: ") context(mytype) context("; its value is: ") 
lxml.text(t, "d")
     lookuptable[mytype] = mytype .. " = " .. myvalue
   else
     context("This Type is already known: ") context(lookuptable[mytype])
   end
end
\stopluacode

\starttext
\xmlprocessbuffer{main}{test}{}

\stoptext


More information about the ntg-context mailing list