ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* xml in lua: how to say "ancestor"?
@ 2017-02-25 16:18 Thomas A. Schmitz
  2017-02-25 18:30 ` Hans Hagen
  0 siblings, 1 reply; 9+ messages in thread
From: Thomas A. Schmitz @ 2017-02-25 16:18 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xml in lua: how to say "ancestor"?
  2017-02-25 16:18 xml in lua: how to say "ancestor"? Thomas A. Schmitz
@ 2017-02-25 18:30 ` Hans Hagen
  2017-02-25 19:24   ` Thomas A. Schmitz
  0 siblings, 1 reply; 9+ messages in thread
From: Hans Hagen @ 2017-02-25 18:30 UTC (permalink / raw)
  To: ntg-context

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
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xml in lua: how to say "ancestor"?
  2017-02-25 18:30 ` Hans Hagen
@ 2017-02-25 19:24   ` Thomas A. Schmitz
  2017-02-25 20:10     ` Hans Hagen
  0 siblings, 1 reply; 9+ messages in thread
From: Thomas A. Schmitz @ 2017-02-25 19:24 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xml in lua: how to say "ancestor"?
  2017-02-25 19:24   ` Thomas A. Schmitz
@ 2017-02-25 20:10     ` Hans Hagen
  2017-02-25 20:35       ` Thomas A. Schmitz
  0 siblings, 1 reply; 9+ messages in thread
From: Hans Hagen @ 2017-02-25 20:10 UTC (permalink / raw)
  To: ntg-context

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
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xml in lua: how to say "ancestor"?
  2017-02-25 20:10     ` Hans Hagen
@ 2017-02-25 20:35       ` Thomas A. Schmitz
  2017-02-25 20:49         ` Hans Hagen
  0 siblings, 1 reply; 9+ messages in thread
From: Thomas A. Schmitz @ 2017-02-25 20:35 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xml in lua: how to say "ancestor"?
  2017-02-25 20:35       ` Thomas A. Schmitz
@ 2017-02-25 20:49         ` Hans Hagen
  2017-02-25 21:15           ` Thomas A. Schmitz
  2017-03-03 16:47           ` Thomas A. Schmitz
  0 siblings, 2 replies; 9+ messages in thread
From: Hans Hagen @ 2017-02-25 20:49 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xml in lua: how to say "ancestor"?
  2017-02-25 20:49         ` Hans Hagen
@ 2017-02-25 21:15           ` Thomas A. Schmitz
  2017-03-03 16:47           ` Thomas A. Schmitz
  1 sibling, 0 replies; 9+ messages in thread
From: Thomas A. Schmitz @ 2017-02-25 21:15 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xml in lua: how to say "ancestor"?
  2017-02-25 20:49         ` Hans Hagen
  2017-02-25 21:15           ` Thomas A. Schmitz
@ 2017-03-03 16:47           ` Thomas A. Schmitz
  2017-03-04  9:37             ` Hans Hagen
  1 sibling, 1 reply; 9+ messages in thread
From: Thomas A. Schmitz @ 2017-03-03 16:47 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xml in lua: how to say "ancestor"?
  2017-03-03 16:47           ` Thomas A. Schmitz
@ 2017-03-04  9:37             ` Hans Hagen
  0 siblings, 0 replies; 9+ messages in thread
From: Hans Hagen @ 2017-03-04  9:37 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-03-04  9:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-25 16:18 xml in lua: how to say "ancestor"? Thomas A. Schmitz
2017-02-25 18:30 ` Hans Hagen
2017-02-25 19:24   ` Thomas A. Schmitz
2017-02-25 20:10     ` Hans Hagen
2017-02-25 20:35       ` Thomas A. Schmitz
2017-02-25 20:49         ` Hans Hagen
2017-02-25 21:15           ` Thomas A. Schmitz
2017-03-03 16:47           ` Thomas A. Schmitz
2017-03-04  9:37             ` Hans Hagen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).