ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* processing xml with lua
@ 2013-02-26 12:23 Schmitz Thomas A.
  2013-02-26 12:54 ` Hans Hagen
  0 siblings, 1 reply; 8+ messages in thread
From: Schmitz Thomas A. @ 2013-02-26 12:23 UTC (permalink / raw)
  To: mailing ConTeXt users list for

Hi all,

one of my favorite topics… Here is a minimal example which shows something that I don't understand: when I process the subsection with the lua code, I want to get the value of the current section's "label" attribute in xml.attribute(r, "../../section", "label", "X"). So i was expecting that lua would go up (../../) and fetch the current section's label. But if you run the example, you see that it actually always gets the first section's label (value is always 1). How can I get the current section's label with a lua function?

THanks and all best

Thomas

\startbuffer[test]
<chapter>
  <section label="1">
    <subsection label="1">
      <content>
        text 1.1
      </content>
    </subsection>
  </section>
  <section label="3">
    <subsection label="1">
      <content>
        text 3.1
      </content>
    </subsection>
    <subsection label="5">
      <content>
        text 3.5
      </content>
    </subsection>
  </section>
</chapter>
\stopbuffer

\startxmlsetups xml:testsetups
	\xmlsetsetup{\xmldocument}{chapter|section|subsection|content}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:testsetups}

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

\startxmlsetups xml:section
	\xmlflush{#1} \par
\stopxmlsetups

\startxmlsetups xml:subsection
	\xmlfunction{#1}{test}
\stopxmlsetups

\startluacode
outfile = io.open('temp.tmp', 'w')
function xml.functions.test(r)
  content = xml.text(r, "content")
  section = xml.attribute(r, "../../section", "label", "X")
  context("This ") context(content) context(" is in section ") context(section)
end
\stopluacode

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

\enabletrackers[context.trace]

\xmlshow{main}
\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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: processing xml with lua
  2013-02-26 12:23 processing xml with lua Schmitz Thomas A.
@ 2013-02-26 12:54 ` Hans Hagen
  2013-02-26 13:32   ` Schmitz Thomas A.
  0 siblings, 1 reply; 8+ messages in thread
From: Hans Hagen @ 2013-02-26 12:54 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Schmitz Thomas A.

On 2/26/2013 1:23 PM, Schmitz Thomas A. wrote:
> Hi all,
>
> one of my favorite topics… Here is a minimal example which shows something that I don't understand: when I process the subsection with the lua code, I want to get the value of the current section's "label" attribute in xml.attribute(r, "../../section", "label", "X"). So i was expecting that lua would go up (../../) and fetch the current section's label. But if you run the example, you see that it actually always gets the first section's label (value is always 1). How can I get the current section's label with a lua function?

you go up to the parent of sections which to far up, try:

   section = xml.attribute(r, "..", "label", "X")

or

   section = xml.filter(r, "../attribute(label)")

> THanks and all best
>
> Thomas
>
> \startbuffer[test]
> <chapter>
>    <section label="1">
>      <subsection label="1">
>        <content>
>          text 1.1
>        </content>
>      </subsection>
>    </section>
>    <section label="3">
>      <subsection label="1">
>        <content>
>          text 3.1
>        </content>
>      </subsection>
>      <subsection label="5">
>        <content>
>          text 3.5
>        </content>
>      </subsection>
>    </section>
> </chapter>
> \stopbuffer
>
> \startxmlsetups xml:testsetups
> 	\xmlsetsetup{\xmldocument}{chapter|section|subsection|content}{xml:*}
> \stopxmlsetups
>
> \xmlregistersetup{xml:testsetups}
>
> \startxmlsetups xml:chapter
> 	\xmlflush{#1}
> \stopxmlsetups
>
> \startxmlsetups xml:section
> 	\xmlflush{#1} \par
> \stopxmlsetups
>
> \startxmlsetups xml:subsection
> 	\xmlfunction{#1}{test}
> \stopxmlsetups
>
> \startluacode
> outfile = io.open('temp.tmp', 'w')
> function xml.functions.test(r)
>    content = xml.text(r, "content")
>    section = xml.attribute(r, "../../section", "label", "X")
>    context("This ") context(content) context(" is in section ") context(section)
> end
> \stopluacode
>
> \starttext
> \xmlprocessbuffer{main}{test}{}
>
> \enabletrackers[context.trace]
>
> \xmlshow{main}
> \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://tex.aanhet.net
> archive  : http://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________
>


-- 

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | 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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: processing xml with lua
  2013-02-26 12:54 ` Hans Hagen
@ 2013-02-26 13:32   ` Schmitz Thomas A.
  2013-03-05  8:47     ` Thomas A. Schmitz
  0 siblings, 1 reply; 8+ messages in thread
From: Schmitz Thomas A. @ 2013-02-26 13:32 UTC (permalink / raw)
  To: mailing list for ConTeXt users


On Feb 26, 2013, at 1:54 PM, Hans Hagen <pragma@wxs.nl> wrote:

> you go up to the parent of sections which to far up, try:
> 
>  section = xml.attribute(r, "..", "label", "X")
> 
> or
> 
>  section = xml.filter(r, "../attribute(label)")
> 
> 

Mojca, Hans,

thanks, that is exactly right! I wasn't too sure about ../../section, but now it makes sense. I have to study xpath a bit more, I guess.

All best

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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: processing xml with lua
  2013-02-26 13:32   ` Schmitz Thomas A.
@ 2013-03-05  8:47     ` Thomas A. Schmitz
  2013-03-05  8:58       ` Hans Hagen
  0 siblings, 1 reply; 8+ messages in thread
From: Thomas A. Schmitz @ 2013-03-05  8:47 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________
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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: processing xml with lua
  2013-03-05  8:47     ` Thomas A. Schmitz
@ 2013-03-05  8:58       ` Hans Hagen
  2013-03-05  9:16         ` Thomas A. Schmitz
  0 siblings, 1 reply; 8+ messages in thread
From: Hans Hagen @ 2013-03-05  8:58 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Thomas A. Schmitz

On 3/5/2013 9:47 AM, Thomas A. Schmitz wrote:
> 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).

     lookuptable[mytype] = mytype .. " = " .. myvalue
     inspect(lookuptable)

the .. triggers a tostring on myvalue which in turn serializes the xml

     lookuptable[mytype] = { mytype = myvalue }

would keep myvalue as xml node


> 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
> ___________________________________________________________________________________
>
> 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://tex.aanhet.net
> archive  : http://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________
>


-- 

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | 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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: processing xml with lua
  2013-03-05  8:58       ` Hans Hagen
@ 2013-03-05  9:16         ` Thomas A. Schmitz
  2013-03-05 10:11           ` Hans Hagen
  0 siblings, 1 reply; 8+ messages in thread
From: Thomas A. Schmitz @ 2013-03-05  9:16 UTC (permalink / raw)
  To: Hans Hagen; +Cc: mailing list for ConTeXt users

On 03/05/2013 09:58 AM, Hans Hagen wrote:
>      lookuptable[mytype] = mytype .. " = " .. myvalue
>      inspect(lookuptable)
>
> the .. triggers a tostring on myvalue which in turn serializes the xml
>
>      lookuptable[mytype] = { mytype = myvalue }
>
> would keep myvalue as xml node

Hans, thanks a lot, the explanation makes sense, but your suggestion is 
not yet clear enough for me: how would I then process the node?

context(lookuptable[mytype]["mytype"])

gives again the serialized xml.

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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: processing xml with lua
  2013-03-05  9:16         ` Thomas A. Schmitz
@ 2013-03-05 10:11           ` Hans Hagen
  2013-03-05 10:27             ` Thomas A. Schmitz
  0 siblings, 1 reply; 8+ messages in thread
From: Hans Hagen @ 2013-03-05 10:11 UTC (permalink / raw)
  To: Thomas A. Schmitz; +Cc: mailing list for ConTeXt users

On 3/5/2013 10:16 AM, Thomas A. Schmitz wrote:
> On 03/05/2013 09:58 AM, Hans Hagen wrote:
>>      lookuptable[mytype] = mytype .. " = " .. myvalue
>>      inspect(lookuptable)
>>
>> the .. triggers a tostring on myvalue which in turn serializes the xml
>>
>>      lookuptable[mytype] = { mytype = myvalue }
>>
>> would keep myvalue as xml node
>
> Hans, thanks a lot, the explanation makes sense, but your suggestion is
> not yet clear enough for me: how would I then process the node?
>
> context(lookuptable[mytype]["mytype"])
>
> gives again the serialized xml.

\startluacode
local lookuptable = { }
function xml.functions.lookup(t)
     local mytype  = xml.text(t, "c")
     local myvalue = lookuptable[mytype]
     if not myvalue then
         myvalue = xml.first(t, "d")
         lookuptable[mytype] = myvalue
         context("registered: ")
     else
         context("reused: ")
     end
     -- each found node gets an id
     lxml.flush(lxml.id(myvalue))
     context(" or ")
     -- in tex mode a to-tex serializer is the default
     xml.cprint(myvalue)
end
\stopluacode

One place to look for tricks is in x-mathml.mkiv / lua (I know, I should 
finish that manual).

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | 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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: processing xml with lua
  2013-03-05 10:11           ` Hans Hagen
@ 2013-03-05 10:27             ` Thomas A. Schmitz
  0 siblings, 0 replies; 8+ messages in thread
From: Thomas A. Schmitz @ 2013-03-05 10:27 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Hans Hagen

On 03/05/2013 11:11 AM, Hans Hagen wrote:
> \startluacode
> local lookuptable = { }
> function xml.functions.lookup(t)
>      local mytype  = xml.text(t, "c")
>      local myvalue = lookuptable[mytype]
>      if not myvalue then
>          myvalue = xml.first(t, "d")
>          lookuptable[mytype] = myvalue
>          context("registered: ")
>      else
>          context("reused: ")
>      end
>      -- each found node gets an id
>      lxml.flush(lxml.id(myvalue))
>      context(" or ")
>      -- in tex mode a to-tex serializer is the default
>      xml.cprint(myvalue)
> end
> \stopluacode
>
> One place to look for tricks is in x-mathml.mkiv / lua (I know, I should
> finish that manual).
>
> Hans

Beautiful! Thanks a lot, that's just what I needed. And yes, in the 
medium-to-long run, finishing the manual will save you from being 
pestered with my questions... :-)

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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

end of thread, other threads:[~2013-03-05 10:27 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-26 12:23 processing xml with lua Schmitz Thomas A.
2013-02-26 12:54 ` Hans Hagen
2013-02-26 13:32   ` Schmitz Thomas A.
2013-03-05  8:47     ` Thomas A. Schmitz
2013-03-05  8:58       ` Hans Hagen
2013-03-05  9:16         ` Thomas A. Schmitz
2013-03-05 10:11           ` Hans Hagen
2013-03-05 10:27             ` Thomas A. Schmitz

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).