DOM / XPath combination

P

Phoebian

Hi all,

Here is a little concept problem, and I need some guideline. I am
currently developing a application which delivers XML files. I want
then to sequentially perform some actions on selected part of that XML
file through a XPath expression. These action have actually to modify
the structure of the XML file itself and that seems to me like a big
hurdle. Maybe I am wrong but I will try to explain what I want to
do...

the XML file ressembles that... very roughly :)

<Root>
<Obj1>
<Head action="yes">blah</Head>
<Body>blah</Body>
<Foot action="yes">blah</Foot>
</Obj1>
...
<Obj2>
<Head>blah</Head>
<Body action="yes">blah</Body>
<Foot>blah</Foot>
</Obj2>
</Root>

let say my XPath expression is '//*[@action="yes"]'. Now for each of
the 3 concerned element I want to do some formatting...

<Root>
<Obj1>
<Head action="yes">
<bonus>yo</bonus> <--- added as part of the formatting
<text>blah<text> <--- formatting
</Head>
<Body>blah</Body>
<Foot action="yes">
<text>blah<text> <--- formatting
<bonus>yo</bonus> <--- added as part of the formatting
</Foot>
</Obj1>
...
<Obj2>
<Head>blah</Head>
<Body action="yes">
<text>blah<text> <--- formatting
</Body>
<Foot>blah</Foot>
</Obj2>
</Root>

Here is my problem. I first convert my XML file to a DOM
representation, in order to be able to do sequential processing.
Indeed a mere XSLT transfo is of no help since I want to be able to do
many thing between two actions. I then do the XPath evaluation but
further modification of the resulting nodelist will never be reflected
in the original XML files. I should say that I am using python 2.3,
and Domlette/XPath from 4suite to do that.

My question is:

How can I use the resulting XPath result as a live representation of
the underlying XML nodes (DOM concept) so that I can both perform
sequential action (by traversing the resulting xpath nodelist) and
reflects modifications done on those same nodes in the original XML
file?

Maybe I am wrong on the live object concept of the DOM but I thought
it would be possible. Any idea to achieve that would be welcome...

Phoebian
 
U

Uche Ogbuji

How can I use the resulting XPath result as a live representation of
the underlying XML nodes (DOM concept) so that I can both perform
sequential action (by traversing the resulting xpath nodelist) and
reflects modifications done on those same nodes in the original XML
file?

Maybe I am wrong on the live object concept of the DOM but I thought
it would be possible. Any idea to achieve that would be welcome...

Actually, the results of 4XPath node set expressions are lists of
actual Domlette nodes, and you can use the mutation API on these
nodes. For the API see:

http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/domlettes

You can then use Domlette.Print(node) to reflect the updates back to
XML.

Did you try any particular mutations with no effect (or ill effect)?

FWIW, you might want to consider XUpdate, which is also implemented in
4Suite. I happen to cover it in my most recent ORA article:

http://www.xml.com/pub/a/2004/05/12/py-xml.html

--Uche
http://uche.ogbuji.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top