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 wiki page. I do understand the code better now, thank you.

Jano