xml in lua: how to say "ancestor"?
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 Thomas
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 -----------------------------------------------------------------
Hans, thanks for your efforts and your help - but I need a lua solution because I have to process my document in lua tables. Thanks Thomas On 02/25/2017 07:30 PM, Hans Hagen wrote:
\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
On 2/25/2017 8:24 PM, Thomas A. Schmitz wrote:
Hans,
thanks for your efforts and your help - but I need a lua solution because I have to process my document in lua tables.
then you can just use xml.attribute (or lxml.attribute)
Thanks
Thomas
On 02/25/2017 07:30 PM, Hans Hagen wrote:
\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
___________________________________________________________________________________
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 ___________________________________________________________________________________
-- ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
On 02/25/2017 09:10 PM, Hans Hagen wrote:
then you can just use
xml.attribute (or lxml.attribute)
Yes, exactly: how does something like \xmlattribute{#1}{..[tag()='section' or tag()='chapter']}{title} translate into lua? such as xml.attribute(e, "../../chapter", "title", "")? Thomas
On 2/25/2017 9:35 PM, Thomas A. Schmitz wrote:
On 02/25/2017 09:10 PM, Hans Hagen wrote:
then you can just use
xml.attribute (or lxml.attribute)
Yes, exactly: how does something like
\xmlattribute{#1}{..[tag()='section' or tag()='chapter']}{title}
translate into lua? such as
xml.attribute(e, "../../chapter", "title", "")?
xml.attribute(e,"..[tag()='section' or tag()='chapter']","title") with an optional default (there is also p = xml.parent(e) and then you can access p.at.title .. so there are several ways( Hans
Thomas ___________________________________________________________________________________
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 ___________________________________________________________________________________
-- ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
On 02/25/2017 09:49 PM, Hans Hagen wrote:
xml.attribute(e,"..[tag()='section' or tag()='chapter']","title")
with an optional default
(there is also p = xml.parent(e) and then you can access p.at.title .. so there are several ways(
Excellent, I can work with that! Thanks a lot Thomas
On 25.02.2017 21:49, Hans Hagen wrote:
xml.attribute(e,"..[tag()='section' or tag()='chapter']","title")
with an optional default
(there is also p = xml.parent(e) and then you can access p.at.title .. so there are several ways(
Hans
I am sorry it took me so long to play with this... But it still doesn't do what I need. My problem is still: from a given element, I need to grab the content of an attribute that may be two or three or four levels up in the xml tree. The code below (adapted from Hans) still only works for a parent, but I need access to an "ancestor." Thomas \starttext \startbuffer[test] <document> <chapter title="OneC"> <section title="OneS"> <subsection title="OneSS"> Subsection Text </subsection> </section> </chapter> <chapter title="TwoC"> <subsection title="OneSS"> Subsection Text </subsection> </chapter> </document> \stopbuffer \startluacode function xml.functions.xml_subsection(e) chaptertitle = xml.attribute(e, "..[tag()='chapter']", "title") context("Title of chapter is: ") context(chaptertitle) context.par() lxml.flush(e) context.blank( { "2*line" }) end \stopluacode \startxmlsetups xml:test:setups \xmlsetsetup{\xmldocument}{*}{xml:*} \stopxmlsetups \xmlregistersetup{xml:test:setups} \startxmlsetups xml:document \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:chapter \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:section \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:subsection \xmlfunction{#1}{xml_subsection} \stopxmlsetups \xmlprocessbuffer{main}{test}{} \stoptext
On 3/3/2017 5:47 PM, Thomas A. Schmitz wrote:
On 25.02.2017 21:49, Hans Hagen wrote:
xml.attribute(e,"..[tag()='section' or tag()='chapter']","title")
with an optional default
(there is also p = xml.parent(e) and then you can access p.at.title .. so there are several ways(
Hans
I am sorry it took me so long to play with this... But it still doesn't do what I need. My problem is still: from a given element, I need to grab the content of an attribute that may be two or three or four levels up in the xml tree. The code below (adapted from Hans) still only works for a parent, but I need access to an "ancestor."
chaptertitle = xml.attribute(e, "..[tag()='chapter']", "title") if not chaptertitle or chaptertitle == "" then chaptertitle = xml.attribute(e, "../..[tag()='chapter']", "title") end
Thomas
\starttext
\startbuffer[test] <document> <chapter title="OneC"> <section title="OneS"> <subsection title="OneSS"> Subsection Text </subsection> </section> </chapter> <chapter title="TwoC"> <subsection title="OneSS"> Subsection Text </subsection> </chapter> </document> \stopbuffer
\startluacode
function xml.functions.xml_subsection(e) chaptertitle = xml.attribute(e, "..[tag()='chapter']", "title") context("Title of chapter is: ") context(chaptertitle) context.par() lxml.flush(e) context.blank( { "2*line" }) end
\stopluacode
\startxmlsetups xml:test:setups \xmlsetsetup{\xmldocument}{*}{xml:*} \stopxmlsetups
\xmlregistersetup{xml:test:setups}
\startxmlsetups xml:document \xmlflush{#1} \stopxmlsetups
\startxmlsetups xml:chapter \xmlflush{#1} \stopxmlsetups
\startxmlsetups xml:section \xmlflush{#1} \stopxmlsetups
\startxmlsetups xml:subsection \xmlfunction{#1}{xml_subsection} \stopxmlsetups
\xmlprocessbuffer{main}{test}{}
\stoptext ___________________________________________________________________________________
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 ___________________________________________________________________________________
-- ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
participants (2)
-
Hans Hagen
-
Thomas A. Schmitz