[xml] Question about parent/child (python-libxml2)


how can I get the direct children of a node?  The following

#!/usr/bin/env python
import libxml2
dom = libxml2.parseDoc('''<?xml version="1.0"?>
<para role="x">Then, shalt thou count to
<emphasis>three</emphasis>, no more, no less.</para>''')
nodes = dom.xpathEval("//*[ role='x']")
for n in nodes:
    if n.children:
        for c in n.children:
            if c.name == "comment":
            print c.serialize()

prints out:

Then, shalt thou count to

, no more, no less.

I like to print only the direct children, i.e. the second
last line with content 'three' should not be printed.  How
can I achieve that?  Thanks in advance!

W. Borgert <debacle debian org>, http://people.debian.org/~debacle/

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]