[NTG-context] Xml filtering in Lua

Duncan Hothersall dh at capdm.com
Wed Nov 16 17:18:20 CET 2022


Hi Thomas.

I'm not sure about the code, sorry, but I do know that an XML document
can't have two IDs of the same value. Typically you would use a linkend
attribute on the element which is referencing an id (in this case the
topics, I think).

Probably doesn't help with your problem, but it's likely a prerequisite for
it to work.

Bests,

Duncan

On Wed, 16 Nov 2022 at 16:11, Thomas A. Schmitz via ntg-context <
ntg-context at ntg.nl> wrote:

> Hi all,
>
> I thought this was easy, but I overestimated my competence… I want to
> filter xml elements via their attributes and retrieve and typeset parts
> belonging together. Here is a small test file that explains what I’m trying:
>
> \startbuffer[test]
> <document>
>    <topics>
>       <topic id="test1">
>         <title>This is the first test</title>
>         <date>22/11/16</date>
>       </topic>
>       <topic id="test2">
>         <title>This is the second test</title>
>         <date>22/11/17</date>
>       </topic>
>    </topics>
>    <chapters>
>       <chapter id="test1">
>         <content>
>           This will be the content of the <emph>first</emph> chapter.
>         </content>
>       </chapter>
>       <chapter id="test2">
>          <content>
>           This will be the content of the <emph>second</emph> chapter.
>          </content>
>       </chapter>
>    </chapters>
> </document>
> \stopbuffer
>
> \startxmlsetups xml:testsetups
>         \xmlsetsetup{#1}{*}{-}
>         \xmlsetsetup{#1}{document|chapters|chapter|content|emph}{xml:*}
> \stopxmlsetups
>
> \xmlregistersetup{xml:testsetups}
>
> \startxmlsetups xml:document
>         \xmlflush{#1}
> \stopxmlsetups
>
> \startxmlsetups xml:chapters
>         \xmlflush{#1}
> \stopxmlsetups
>
> \startxmlsetups xml:chapter
>         \xmlfunction {#1} {chapter}
>          \xmlflush {#1}
> \stopxmlsetups
>
> \startxmlsetups xml:chapter:content
>         \xmltext {#1} {content}
> \stopxmlsetups
>
> \startxmlsetups xml:emph
>    {\em \xmlflush {#1}}
> \stopxmlsetups
>
> \startluacode
> function xml.functions.chapter (t)
>          local ch_id = t.at.id
>                 local metadata = xml.filter (root,
> '../../topics/topic[@id=="%s"]', ch_id)
>                 print (inspect(metadata))
>                 lxml.command(t, ".", "xml:chapter:content")
>                 context.par ()
>                 context (ch_id)
>                 context.par ()
> end
> \stopluacode
>
> \starttext
>         \xmlprocessbuffer{main}{test}{}
> \stoptext
>
> The line with xml.filter does not work as I expected. How can I walk the
> tree, find the “topic” element with the same “id” attribute as the chapter
> I’m currently in, and then typeset the different children of the topic
> element?
>
> Thanks a lot and all best
>
> Thomas
>
>
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to
> the Wiki!
>
> maillist : ntg-context at ntg.nl /
> https://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
> archive  : https://bitbucket.org/phg/context-mirror/commits/
> wiki     : https://contextgarden.net
>
> ___________________________________________________________________________________
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ntg.nl/pipermail/ntg-context/attachments/20221116/4065c6f8/attachment.htm>


More information about the ntg-context mailing list