ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Xml filtering in Lua
@ 2022-11-16 16:10 Thomas A. Schmitz via ntg-context
  2022-11-16 16:18 ` Duncan Hothersall via ntg-context
  0 siblings, 1 reply; 13+ messages in thread
From: Thomas A. Schmitz via ntg-context @ 2022-11-16 16:10 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Thomas A. Schmitz

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@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
___________________________________________________________________________________

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

end of thread, other threads:[~2022-11-21  8:23 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-16 16:10 Xml filtering in Lua Thomas A. Schmitz via ntg-context
2022-11-16 16:18 ` Duncan Hothersall via ntg-context
2022-11-16 17:33   ` Thomas A. Schmitz via ntg-context
2022-11-16 18:56     ` mf via ntg-context
2022-11-16 19:55       ` Thomas A. Schmitz via ntg-context
2022-11-16 20:51         ` Denis Maier via ntg-context
2022-11-16 21:09           ` Thomas A. Schmitz via ntg-context
2022-11-16 22:19             ` Hans Hagen via ntg-context
2022-11-16 19:56       ` mf via ntg-context
2022-11-17 10:04         ` Hans Hagen via ntg-context
2022-11-17 10:56           ` Thomas A. Schmitz via ntg-context
2022-11-20 18:19           ` Thomas A. Schmitz via ntg-context
2022-11-21  8:23             ` Thomas A. Schmitz via ntg-context

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