ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Olivier Binda <olivier.binda@wanadoo.fr>
To: ntg-context@ntg.nl
Subject: Help with xml & lua
Date: Wed, 2 Aug 2017 17:23:31 +0200	[thread overview]
Message-ID: <48e40256-9b2a-6d7b-ec77-760212d7b472@wanadoo.fr> (raw)

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
___________________________________________________________________________________

             reply	other threads:[~2017-08-02 15:23 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-02 15:23 Olivier Binda [this message]
2017-08-07 13:38 ` Hans Hagen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=48e40256-9b2a-6d7b-ec77-760212d7b472@wanadoo.fr \
    --to=olivier.binda@wanadoo.fr \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).