On 2/25/2017 5:18 PM, Thomas A. Schmitz wrote:
Hi all,
no real example, just snippets, sorry: I need to process xml documents in lua (because I want to collect information in tables). The structure is a bit irregular:
<document> <chapter title="One"> <section title="One"> <subsection title="One"> Text </subsection> </section> </chapter> <chapter title="Two"> <subsection title="One"> Text </subsection> </chapter> </document>
So chapters may or may not have sections. If, on the level of my subscetions, I want to extract the information of the chapter title, in a ConTeXt environment, I could say \xmlattribute{#1}{ancestor::chapter}{title}. How could I do the same in lua? I have something like xml.attribute(e, "../../chapter", "title", "") but that only works for the first case, not the second one. I tried to understand the lpegs in lxml-lpt.lua, but didn't succeed.
Thanks, and best wishes \starttext
\startbuffer[test] <document> <chapter title="OneC"> <section title="OneS"> <subsection title="OneSS"> Text </subsection> </section> </chapter> <chapter title="TwoC"> <subsection title="OneSS"> Text </subsection> </chapter> </document> \stopbuffer \startxmlsetups xml:test:setups \xmlsetsetup{\xmldocument}{*}{xml:*} \stopxmlsetups \xmlregistersetup{xml:test:setups} \startxmlsetups xml:document \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:chapter \blank[2*big] CHAPTER: \xmlattribute{#1}{.}{title}\blank \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:section \blank SECTION: \xmlattribute{#1}{.}{title} \blank\relax (previous:\xmlattribute{#1}{..[tag()='chapter']}{title}) \blank \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:subsection \blank SUBSECTION: \xmlattribute{#1}{.}{title} \blank\relax \blank (previous:\xmlattribute{#1}{..[tag()='section' or tag()='chapter']}{title}) \blank \xmlflush{#1} \stopxmlsetups \xmlprocessbuffer{main}{test}{} \stoptext ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------