ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* content of xml node (element) in lua
@ 2020-10-16 18:57 Jano Kula
  2020-10-16 22:16 ` mf
  0 siblings, 1 reply; 4+ messages in thread
From: Jano Kula @ 2020-10-16 18:57 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 845 bytes --]

Hello!

Processing XML I want to manipulate content of XML elements inside Lua.

I can define the Lua function xml.functions.name(t), where processing
happens, and use the command \xmlfunction{#1}{name} to pass the current
node #1 from ctx to lua as described in the xml-mkiv.pdf manual. To access
the element content xml.text(t,"/element") Lua function is used.

However, using the same function xml.text("#1",/element) directly inside
Lua doesn't work for me. Node #1 is available, but not the content of
/element.

\startluacode
    local node = "#1"
    local content = xml.text("#1","/color")
    context.par() context("node: ") context(node)
    context.par() context("lua: ")  context(content)
\stopluacode

MWE (mkiv, beta) attached.

I haven't found an example, so is it even possible? If so, which way is
more efficient?

Thanks,
Jano

[-- Attachment #1.2: Type: text/html, Size: 1586 bytes --]

[-- Attachment #2: xmlluanode.tex --]
[-- Type: application/octet-stream, Size: 1145 bytes --]

\startbuffer[doc]
<?xml version "1.0"?>
<document>
    <section title="First">
        <color>red</color>
        text1
    </section>
    <section title="Second">
        <color>blue</color>
        text2
    </section>
</document>
\stopbuffer

\startluacode
function xml.functions.name(t)
    local content = xml.text(t,"/color")
    context.par() context("function: ") context(content)
end

\stopluacode

\startxmlsetups xml:name
    \xmlsetsetup{\xmldocument}{*}{-}
    \xmlsetsetup{\xmldocument}{document|section}{xml:name:*}
\stopxmlsetups

\xmlregistersetup{xml:name}

\startxmlsetups xml:name:document
    \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:name:section
    \startsection[title=\xmlatt{#1}{title}]
    % ctx function
    \xmlfunction{#1}{name}
    % lua only
    \startluacode
        local node = "#1"
        local content = xml.text("#1","/color")
        context.par() context("node: ") context(node)
        context.par() context("lua: ")  context(content)
    \stopluacode
    \stopsection
\stopxmlsetups

\starttext

\xmlprocessbuffer{xml:name}{doc}{}

\stoptext

[-- Attachment #3: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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] 4+ messages in thread

* Re: content of xml node (element) in lua
  2020-10-16 18:57 content of xml node (element) in lua Jano Kula
@ 2020-10-16 22:16 ` mf
  2020-10-17 10:02   ` Hans Hagen
  0 siblings, 1 reply; 4+ messages in thread
From: mf @ 2020-10-16 22:16 UTC (permalink / raw)
  To: ntg-context

(sorry, I sent it before refining it)

Il 16/10/20 20:57, Jano Kula ha scritto:
> Hello!
> 
> Processing XML I want to manipulate content of XML elements inside Lua.
> 
> I can define the Lua function xml.functions.name 
> <http://xml.functions.name>(t), where processing happens, and use the 
> command \xmlfunction{#1}{name} to pass the current node #1from ctx to 
> lua as described in the xml-mkiv.pdf manual. To access the element 
> content xml.text(t,"/element") Lua function is used.
> 
> However, using the same function xml.text("#1",/element) directly inside 
> Lua doesn't work for me. Node #1 is available, but not the content of 
> /element.

In your lua fragment, node #1 is available as "xml:name::4" or 
"xml:name::6", but not as a lua table.

When node #1 is passed to a \xmlfunction, the function gets a lua table 
as argument.

To write direct lua code instead of a xmlfunction you'd need a function 
to transform those "xml:name::..." into the lua tables representing the 
xml elements in the xml tree.

Which is what \xmlfunction does, so I wouldn't expect any performance gain.

Massi
___________________________________________________________________________________
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] 4+ messages in thread

* Re: content of xml node (element) in lua
  2020-10-16 22:16 ` mf
@ 2020-10-17 10:02   ` Hans Hagen
  2020-10-17 10:45     ` Jano Kula
  0 siblings, 1 reply; 4+ messages in thread
From: Hans Hagen @ 2020-10-17 10:02 UTC (permalink / raw)
  To: mailing list for ConTeXt users, mf

On 10/17/2020 12:16 AM, mf wrote:
> (sorry, I sent it before refining it)
> 
> Il 16/10/20 20:57, Jano Kula ha scritto:
>> Hello!
>>
>> Processing XML I want to manipulate content of XML elements inside Lua.
>>
>> I can define the Lua function xml.functions.name 
>> <http://xml.functions.name>(t), where processing happens, and use the 
>> command \xmlfunction{#1}{name} to pass the current node #1from ctx to 
>> lua as described in the xml-mkiv.pdf manual. To access the element 
>> content xml.text(t,"/element") Lua function is used.
>>
>> However, using the same function xml.text("#1",/element) directly 
>> inside Lua doesn't work for me. Node #1 is available, but not the 
>> content of /element.
> 
> In your lua fragment, node #1 is available as "xml:name::4" or 
> "xml:name::6", but not as a lua table.

when you're at the lua end you can use lxml.getid to access that node, 
assuming that you somehow pass #1 (being a string)


> When node #1 is passed to a \xmlfunction, the function gets a lua table 
> as argument.
> 
> To write direct lua code instead of a xmlfunction you'd need a function 
> to transform those "xml:name::..." into the lua tables representing the 
> xml elements in the xml tree.
> 
> Which is what \xmlfunction does, so I wouldn't expect any performance gain.

actually one has the same function at the lua end, so once you passed 
the node id you can use

xml.first(lxml.getid("xml:name::4"),"foo/bar")

and alike (the lxml.* function pipe back to tex)

Hans

-----------------------------------------------------------------
                                           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] 4+ messages in thread

* Re: content of xml node (element) in lua
  2020-10-17 10:02   ` Hans Hagen
@ 2020-10-17 10:45     ` Jano Kula
  0 siblings, 0 replies; 4+ messages in thread
From: Jano Kula @ 2020-10-17 10:45 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 2148 bytes --]

On Sat, 17 Oct 2020 at 12:02, Hans Hagen <j.hagen@xs4all.nl> wrote:

> On 10/17/2020 12:16 AM, mf wrote:
> > (sorry, I sent it before refining it)
> >
> > Il 16/10/20 20:57, Jano Kula ha scritto:
> >> Hello!
> >>
> >> Processing XML I want to manipulate content of XML elements inside Lua.
> >>
> >> I can define the Lua function xml.functions.name
> >> <http://xml.functions.name>(t), where processing happens, and use the
> >> command \xmlfunction{#1}{name} to pass the current node #1from ctx to
> >> lua as described in the xml-mkiv.pdf manual. To access the element
> >> content xml.text(t,"/element") Lua function is used.
> >>
> >> However, using the same function xml.text("#1",/element) directly
> >> inside Lua doesn't work for me. Node #1 is available, but not the
> >> content of /element.
> >
> > In your lua fragment, node #1 is available as "xml:name::4" or
> > "xml:name::6", but not as a lua table.
>
> when you're at the lua end you can use lxml.getid to access that node,
> assuming that you somehow pass #1 (being a string)
>
>
> > When node #1 is passed to a \xmlfunction, the function gets a lua table
> > as argument.
> >
> > To write direct lua code instead of a xmlfunction you'd need a function
> > to transform those "xml:name::..." into the lua tables representing the
> > xml elements in the xml tree.
> >
> > Which is what \xmlfunction does, so I wouldn't expect any performance
> gain.
>
> actually one has the same function at the lua end, so once you passed
> the node id you can use
>
> xml.first(lxml.getid("xml:name::4"),"foo/bar")
>
> and alike (the lxml.* function pipe back to tex)
>
> Hans
>
>
Hello,

just finished the exploration of the \xmlfunction definition based on
Massi's advice when Hans' answer arrived. This is the working code:

    \startluacode
        local node = "#1"
        local content = xml.text(*lxml.getid(*"#1"*)*,"/color")
        context.par() context("node: ") context(node)
        context.par() context("lua: ")  context(content)
    \stopluacode

Added a note to XML Lua <https://wiki.contextgarden.net/XML_Lua> wiki page.
I do understand the code better now, thank you.

Jano

[-- Attachment #1.2: Type: text/html, Size: 3310 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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] 4+ messages in thread

end of thread, other threads:[~2020-10-17 10:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-16 18:57 content of xml node (element) in lua Jano Kula
2020-10-16 22:16 ` mf
2020-10-17 10:02   ` Hans Hagen
2020-10-17 10:45     ` Jano Kula

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