[NTG-context] Which lua XML library is embedded in LMTX?

Taco Hoekwater taco at elvenkind.com
Fri Jun 26 13:42:11 CEST 2020


> On 25 Jun 2020, at 12:37, Gerben Wierda <Gerben.Wierda at rna.nl> wrote:
> According to the XML manual:
> << 2.1 >> TEX versus LUA
> It will not come as a surprise that we can access xml files from TEX as well as from Lua. In fact there are two methods to deal with xml in Lua. First there are the low level xml functions in the xml namespace. On top of those functions there is a set of functions in the lxml namespace that deals with xml in a more TEXie way.
> << 3.7 >> manipulation
> Having read that, I was loooking for the documentation of the commands that are available in the xml namespace. I want to manipulate a XML tree in LMTX lua. But looking online gives me all kinds of different XML-solutions that are proposed for lua. Which one is embedded in LMTX and available in the xml namespace? But then I get confused because other elements like xml.first, xml.collected are in the xml namespace and mentioned.

None of the prefab solutions are used. The XML parser in ConTeXt is hand-written by Hans.

Most (all?) of the TeX macros like \xmlfirst are in fact thin wrappers around lua code,
see https://source.contextgarden.net/tex/context/base/mkiv/lxml-ini.lua

However, these are currently not documented. Hans wrote the xml-mkiv manual with TeX users
in mind, so there is very little documentation on the Lua side of things. I have plans myself
to write that ‘missing chapter’ but there are a lot of other things on my list of todos as well.

> What I need (in LMTX lua) to add (sub)nodes to the XML node (that I read with xml.first()) because the data I get is broken and I need to fix it before I can use it (and I cannot get good data) The easiest way is to add the correct data to the XML and then use it in my code. If I cannot do that I need to write parallel code that largely does the same as what I already have (ugh)..

Regardless of whether it is doable in principle, this is not the intended use of the 
xml processing in ConTeXT. ConTeXt XML support is for Formatting, not Transformation. 
If you need to actually change the XML structure, I strongly advice to find a solution 

ConTeXt not only does formatting, but it tries to do this as efficiently and fast
as possible. That means that if you want to modify a parsed tree, you will almost
certainly also have to fix up external indices into that tree and you probably
will have to write the lua mutation functions yourself as well.

So, better look at some binding for libxml2 to some language you like, and then transform
the XML beforehand into something you can confidently use as source for typesetting.

Again: ConTeXt XML support is not meant for XML Transformations.

(my two cents, at least)

Best wishes,

More information about the ntg-context mailing list