ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Help with xml & lua
@ 2017-08-02 15:23 Olivier Binda
  2017-08-07 13:38 ` Hans Hagen
  0 siblings, 1 reply; 2+ messages in thread
From: Olivier Binda @ 2017-08-02 15:23 UTC (permalink / raw)
  To: ntg-context

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



___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Help with xml & lua
  2017-08-02 15:23 Help with xml & lua Olivier Binda
@ 2017-08-07 13:38 ` Hans Hagen
  0 siblings, 0 replies; 2+ messages in thread
From: Hans Hagen @ 2017-08-07 13:38 UTC (permalink / raw)
  To: ntg-context

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
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

end of thread, other threads:[~2017-08-07 13:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-02 15:23 Help with xml & lua Olivier Binda
2017-08-07 13:38 ` Hans Hagen

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