From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/116478 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Duncan Hothersall via ntg-context Newsgroups: gmane.comp.tex.context Subject: Re: Xml filtering in Lua Date: Wed, 16 Nov 2022 16:18:20 +0000 Message-ID: References: Reply-To: mailing list for ConTeXt users Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============6541568447314518818==" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="28161"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Duncan Hothersall , "Thomas A. Schmitz" To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Wed Nov 16 17:19:07 2022 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane-mx.org Original-Received: from zapf.boekplan.nl ([5.39.185.232] helo=zapf.ntg.nl) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ovL7y-00075Q-U2 for gctc-ntg-context-518@m.gmane-mx.org; Wed, 16 Nov 2022 17:19:06 +0100 Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 98FB02A4C14; Wed, 16 Nov 2022 17:18:40 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id gBvr0LZHrVID; Wed, 16 Nov 2022 17:18:38 +0100 (CET) Original-Received: from zapf.ntg.nl (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id B7BE72A4C1E; Wed, 16 Nov 2022 17:18:38 +0100 (CET) Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 1BDA32A4C1C for ; Wed, 16 Nov 2022 17:18:38 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id yFuOv9LtYxat for ; Wed, 16 Nov 2022 17:18:36 +0100 (CET) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=209.85.218.46; helo=mail-ej1-f46.google.com; envelope-from=dh@capdm.com; receiver= Original-Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by zapf.ntg.nl (Postfix) with ESMTPS id CD10D2A4C14 for ; Wed, 16 Nov 2022 17:18:36 +0100 (CET) Original-Received: by mail-ej1-f46.google.com with SMTP id gv23so12591056ejb.3 for ; Wed, 16 Nov 2022 08:18:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=capdm.com; s=google; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=M5Ja8S8czpOIdMlgCE2wtPcvD8Ud8kSgK/RymFGZnD4=; b=bY/xsc5r7Oc7tnV1P6pAJ/S9tWibYEshVKuTb29QpOoX7OkKNgEFA2xKvhGPS9mPRe gezYpBPOFh4gujecpA9dfVlIMjWpb3ahtH4KYKW6lr9Yt/13jQL3WWutz1xP53LRgh+G HynQA89ir8EpUOYs9Z/mBbKm21+by3c1UqrFs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=M5Ja8S8czpOIdMlgCE2wtPcvD8Ud8kSgK/RymFGZnD4=; b=G/7l1vQ/yj+rV4SihX2NfYDWjUAs+g/ApYfn5aR1ysWBv03DeW1pOPAI7twSOlmxc6 8/X2gcWfnq4h7e7FH8FIvvEFgRBdNQXsv0JDDq40gDQQH7Vhcwc0T8RBumjS8xYcMUB+ x/iUEQgxGRshAMy3U83XOSHMR+lZ92+Ol5zH+Nmc+yjdmgAz8aZfdaeCK2mr7N9NeUke 01ezmtu+ZPaiM8SLIJlEN8KSoCDf6vBQ2FiGhWC16+DwFjIoWOlOE5SvPOJtgt9vQo1t Vodc6UQDffISN0pBHzHr+39/uZ9RCed5zx8Cz9/LQGVGguSWNue6YeE5FeOgfNoQkUz/ kUAA== X-Gm-Message-State: ANoB5pld3znXlB+VXrqO+OGRmJEeuaBPZpBzmZRCtFk/wBaEpZA3uAM/ 7IBH1ByA+2Z/FfTdnkBYIw0v14TgCBwXfNKVMtdfp/Ta1xRcQQ== X-Google-Smtp-Source: AA0mqf7OKbYtNPlb6ue1zOkpuiMY4th2GiTFV+cZcqGDeo6y8ecZEKDPyvyK954mJyAaGjRWcb2IpdXtBdPBuVy0c04= X-Received: by 2002:a17:906:31d4:b0:78d:51c0:d4d1 with SMTP id f20-20020a17090631d400b0078d51c0d4d1mr18241859ejf.161.1668615515348; Wed, 16 Nov 2022 08:18:35 -0800 (PST) In-Reply-To: X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.26 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: "ntg-context" Xref: news.gmane.io gmane.comp.tex.context:116478 Archived-At: --===============6541568447314518818== Content-Type: multipart/alternative; boundary="0000000000000f315205ed98d369" --0000000000000f315205ed98d369 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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@ntg.nl> wrote: > Hi all, > > I thought this was easy, but I overestimated my competence=E2=80=A6 I wan= t to > filter xml elements via their attributes and retrieve and typeset parts > belonging together. Here is a small test file that explains what I=E2=80= =99m trying: > > \startbuffer[test] > > > > This is the first test > 22/11/16 > > > This is the second test > 22/11/17 > > > > > > This will be the content of the first chapter. > > > > > This will be the content of the second chapter. > > > > > \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 =3D t.at.id > local metadata =3D xml.filter (root, > '../../topics/topic[@id=3D=3D"%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 =E2=80=9Ctopic=E2=80=9D element with the same =E2=80=9Cid= =E2=80=9D attribute as the chapter > I=E2=80=99m 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@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 > > _________________________________________________________________________= __________ > --0000000000000f315205ed98d369 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Thomas.

I'm not sure about the c= ode, sorry, but I do know that an XML document can't have two IDs of th= e same value. Typically you would use a linkend attribute on the element wh= ich 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,
<= br>
Duncan

On Wed, 16 Nov 2022 at 16:11, Thomas A. Schmitz v= ia ntg-context <ntg-context@ntg.nl= > wrote:
= Hi all,

I thought this was easy, but I overestimated my competence=E2=80=A6 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=E2=80=99= m trying:

\startbuffer[test]
<document>
=C2=A0 =C2=A0<topics>
=C2=A0 =C2=A0 =C2=A0 <topic id=3D"test1">
=C2=A0 =C2=A0 =C2=A0 =C2=A0 <title>This is the first test</title&g= t;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 <date>22/11/16</date>
=C2=A0 =C2=A0 =C2=A0 </topic>
=C2=A0 =C2=A0 =C2=A0 <topic id=3D"test2">
=C2=A0 =C2=A0 =C2=A0 =C2=A0 <title>This is the second test</title&= gt;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 <date>22/11/17</date>
=C2=A0 =C2=A0 =C2=A0 </topic>
=C2=A0 =C2=A0</topics>
=C2=A0 =C2=A0<chapters>
=C2=A0 =C2=A0 =C2=A0 <chapter id=3D"test1">
=C2=A0 =C2=A0 =C2=A0 =C2=A0 <content>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 This will be the content of the <emph= >first</emph> chapter.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 </content>
=C2=A0 =C2=A0 =C2=A0 </chapter>
=C2=A0 =C2=A0 =C2=A0 <chapter id=3D"test2">
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0<content>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 This will be the content of the <emph= >second</emph> chapter.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0</content>
=C2=A0 =C2=A0 =C2=A0 </chapter>
=C2=A0 =C2=A0</chapters>
</document>
\stopbuffer

\startxmlsetups xml:testsetups
=C2=A0 =C2=A0 =C2=A0 =C2=A0 \xmlsetsetup{#1}{*}{-}
=C2=A0 =C2=A0 =C2=A0 =C2=A0 \xmlsetsetup{#1}{document|chapters|chapter|cont= ent|emph}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:testsetups}

\startxmlsetups xml:document
=C2=A0 =C2=A0 =C2=A0 =C2=A0 \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:chapters
=C2=A0 =C2=A0 =C2=A0 =C2=A0 \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:chapter
=C2=A0 =C2=A0 =C2=A0 =C2=A0 \xmlfunction {#1} {chapter}
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0\xmlflush {#1}
\stopxmlsetups

\startxmlsetups xml:chapter:content
=C2=A0 =C2=A0 =C2=A0 =C2=A0 \xmltext {#1} {content}
\stopxmlsetups

\startxmlsetups xml:emph
=C2=A0 =C2=A0{\em \xmlflush {#1}}
\stopxmlsetups

\startluacode
function xml.functions.chapter (t)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0local ch_id =3D t.at.id
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 local metadata =3D = xml.filter (root, '../../topics/topic[@id=3D=3D"%s"]', ch= _id)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 print (inspect(meta= data))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 lxml.command(t, &qu= ot;.", "xml:chapter:content")
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 context.par ()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 context (ch_id)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 context.par ()
end
\stopluacode

\starttext
=C2=A0 =C2=A0 =C2=A0 =C2=A0 \xmlprocessbuffer{main}{test}{}
\stoptext

The line with xml.filter does not work as I expected. How can I walk the tr= ee, find the =E2=80=9Ctopic=E2=80=9D element with the same =E2=80=9Cid=E2= =80=9D attribute as the chapter I=E2=80=99m 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 t= he Wiki!

maillist : ntg-cont= ext@ntg.nl / https://www.ntg.nl/mailman/listinfo/= ntg-context
webpage=C2=A0 : https://www.pragma-ade.nl / http://context.aanhet.net
archive=C2=A0 :
https://bitbucket.org/phg/context-m= irror/commits/
wiki=C2=A0 =C2=A0 =C2=A0: https://contextgarden.net
___________________________________________________________________________= ________
--0000000000000f315205ed98d369-- --===============6541568447314518818== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX18KSWYgeW91ciBxdWVzdGlvbiBpcyBvZiBpbnRlcmVz dCB0byBvdGhlcnMgYXMgd2VsbCwgcGxlYXNlIGFkZCBhbiBlbnRyeSB0byB0aGUgV2lraSEKCm1h aWxsaXN0IDogbnRnLWNvbnRleHRAbnRnLm5sIC8gaHR0cHM6Ly93d3cubnRnLm5sL21haWxtYW4v bGlzdGluZm8vbnRnLWNvbnRleHQKd2VicGFnZSAgOiBodHRwczovL3d3dy5wcmFnbWEtYWRlLm5s IC8gaHR0cDovL2NvbnRleHQuYWFuaGV0Lm5ldAphcmNoaXZlICA6IGh0dHBzOi8vYml0YnVja2V0 Lm9yZy9waGcvY29udGV4dC1taXJyb3IvY29tbWl0cy8Kd2lraSAgICAgOiBodHRwczovL2NvbnRl eHRnYXJkZW4ubmV0Cl9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCg== --===============6541568447314518818==--