Re: [lxml-dev] Handling processing instructions

On Wed, Oct 28, 2009 at 4:21 AM, Emanuele D'Arrigo <manu3d@gmail.com> wrote:
2009/10/27 Chuck Bearden <cfbearden@gmail.com>
On Tue, Oct 27, 2009 at 1:25 PM, Emanuele D'Arrigo <manu3d@gmail.com> wrote:
1) short of iterating from the root element using repeatedly getprevious(), is there a way to obtain the processing instructions that are listed before
It is possible via XPath:
xmlTree.getroot().xpath('preceding-sibling::node()')
Hi Chuck, thanks for your help!
How is the xpath approach above different from:
xmlTree.getRoot().getprevious()
?
Evidently I mis-typed or mis-read the first time I tried the ElementTree way of getting a PI that precedes the root element. The obvious ElementTree way does indeed work. My mistake.
from lxml import etree from StringIO import StringIO xmlString = '''<?xml version="1.0"?> ... <?version 1.3?> ... <root> ... <branch> ... <leaf/> ... </branch> ... </root>''' xmlTree = etree.parse(StringIO(xmlString)) xmlTree.getroot().xpath('preceding-sibling::node()') [<?version 1.3?>] xmlTree.getroot().getprevious() <?version 1.3?>
And would I be correct in saying one would still need to iterate "upward" to find the very first processing instruction?
I guess I think of .getprevious() and the preceding-sibling XPath axis as horizontal rather than vertical, so I would say that you need to recur or iterate "backwards" to get all nodes preceding the root element.
from lxml import etree from StringIO import StringIO xmlString = '''<?xml version="1.0"?> ... <?first PI?> ... <?version 1.3?> ... <root> ... <branch> ... <leaf/> ... </branch> ... </root>''' xmlTree = etree.parse(StringIO(xmlString)) xmlTree.getroot().xpath('preceding-sibling::node()') [<?first PI?>, <?version 1.3?>] xmlTree.getroot().getprevious() <?version 1.3?> xmlTree.getroot().getprevious().getprevious() <?first PI?> xmlTree.getroot().getprevious().getprevious().getprevious()
Chuck

2009/10/28 Chuck Bearden <cfbearden@gmail.com>
And would I be correct in saying one would still need to iterate "upward" to find the very first processing instruction?
I guess I think of .getprevious() and the preceding-sibling XPath axis as horizontal rather than vertical, so I would say that you need to recur or iterate "backwards" to get all nodes preceding the root element.
Yes, I think we're thinking the same thing. I was thinking "upward" because of the top-bottom orientation of the source code, but "backward" is indeed reasonable in the context of siblings in the tree.. =) Thank you! Now I just hope somebody (Stephan?) will be able to answer question number 2, how to delete a processing instruction from the tree!! Manu

Stefan, I don't know if you missed this thread: is it possible to remove a processing instruction that is a preceding sibling of the root node of an ElementTree? Somehow I can access it via tree.getroot().getprevious() or tree.getroot().itersiblings(preceding=True).next() but I cannot find a way to delete it. A test case to cut&paste: from lxml import etree from StringIO import StringIO tree = etree.parse(StringIO("<?aProcInstrToBeDeleted?><aRoot />")) Thank you! Manu

Emanuele D'Arrigo, 02.11.2009 16:29:
is it possible to remove a processing instruction that is a preceding sibling of the root node of an ElementTree? Somehow I can access it via tree.getroot().getprevious() or tree.getroot().itersiblings(preceding=True).next() but I cannot find a way to delete it.
A test case to cut&paste:
from lxml import etree from StringIO import StringIO tree = etree.parse(StringIO("<?aProcInstrToBeDeleted?><aRoot />"))
I don't think that's currently possible, no. Could you file a bug in the bug tracker? Thanks! Stefan

2009/11/7 Stefan Behnel <stefan_ml@behnel.de>
Emanuele D'Arrigo, 02.11.2009 16:29:
is it possible to remove a processing instruction that is a preceding sibling of the root node of an ElementTree?
I don't think that's currently possible, no. Could you file a bug in the bug tracker?
Done: https://bugs.launchpad.net/lxml/+bug/479613 Manu
participants (3)
-
Chuck Bearden
-
Emanuele D'Arrigo
-
Stefan Behnel