Hello I have been trying for the past 3 days to process files with xml on the lua side (with Context beta 0.70), but I'm at then end of my wits and really need help. On the lua side, I would really like to be able to : 1) transform a tree : replace nodes with other nodes 2) transform a tree : walk it and modify node attributes 3) walk a modified tree and flush it with setups Could someone give me hints or better yet a lua snipet that : 1) load a tree in lua 2) modify it 3) walk it 4) it can be done again I have had some success with 1, but lost 2 days trying to make 2 & 3 work with setups A) I managed to replace a node of an xml document with a subtree of another xml document by doing : -- load the main tree local mainTree = lxml.load("mainTree", "mainFile.xml) local replacementNode = xml.first(lxml.load("replacement", "replacementFile.xml), somePattern) -- replace nodes for node in xml.collected(tree, pattern) do node.tg ="toBeReplaced" xml.replace(tree, "toBeReplaced", replacementNode) end But then I failed to walk the modified tree with setups : my setups for "mainTree" work, but not for the inserted subtrees B) So, to process my tree further, I need to first serialize it -- save the modified tree xml.save(mainTree, "saved.xml") And then, I can reload the modified tree and work further with it (but it sucks to have to save to reload). C) "xml.replace" works in a weird way : it erases node (toBereplaced) and inserts the children of replacementNode as children of the parent of the node to be replaced... D) I did not manage to walk a tree loaded with xml.load in lua. I know that I have to register setups, I tried all sort of things, looked at the Tex/Lua context source code but could not make it work. Also the xml documentation for context on the net is really old, sparse and obsolete. On the tex side, it works : \xmlprocessfile{mySetups}{saved.xml}{} works like a charm But the TeX commands don't allow you to grab a modified xml tree, once you have walked it E) I tried lxml.load(id, filename) context("\\xmlregistereddocumentsetups{" .. id .. "}{}") Best regards, OlivierBinda
On 8/2/2017 5:23 PM, Olivier Binda wrote:
Hello
I have been trying for the past 3 days to process files with xml on the lua side (with Context beta 0.70), but I'm at then end of my wits and really need help.
On the lua side, I would really like to be able to :
1) transform a tree : replace nodes with other nodes
2) transform a tree : walk it and modify node attributes
3) walk a modified tree and flush it with setups
what do you mean with 'walk with setups' ... at the lua end? setuips are something at the tex end
Could someone give me hints or better yet a lua snipet that : 1) load a tree in lua 2) modify it 3) walk it 4) it can be done again
in some cases you need to apply setups after adaption, eg with \xmlregistereddocumentsetups{...}{...}
I have had some success with 1, but lost 2 days trying to make 2 & 3 work with setups
A) I managed to replace a node of an xml document with a subtree of another xml document by doing :
-- load the main tree local mainTree = lxml.load("mainTree", "mainFile.xml)
local replacementNode = xml.first(lxml.load("replacement", "replacementFile.xml), somePattern)
-- replace nodes for node in xml.collected(tree, pattern) do node.tg ="toBeReplaced" xml.replace(tree, "toBeReplaced", replacementNode) end
But then I failed to walk the modified tree with setups : my setups for "mainTree" work, but not for the inserted subtrees
at the lua end there's lxm.include (there's also some some indexing going on)
B) So, to process my tree further, I need to first serialize it
-- save the modified tree
xml.save(mainTree, "saved.xml")
And then, I can reload the modified tree and work further with it (but it sucks to have to save to reload).
\xmlinclude should work ok (at least it does at our end) given that you do it before associating the setups
C) "xml.replace" works in a weird way : it erases node (toBereplaced) and inserts the children of replacementNode as children of the parent of the node to be replaced...
D) I did not manage to walk a tree loaded with xml.load in lua. I know that I have to register setups, I tried all sort of things, looked at the Tex/Lua context source code but could not make it work. Also the xml documentation for context on the net is really old, sparse and obsolete.
On the tex side, it works : \xmlprocessfile{mySetups}{saved.xml}{} works like a charm But the TeX commands don't allow you to grab a modified xml tree, once you have walked it
E) I tried
lxml.load(id, filename) context("\\xmlregistereddocumentsetups{" .. id .. "}{}") you need to provide (small) working (valid) examples
Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
participants (2)
-
Hans Hagen
-
Olivier Binda