[XML-SIG] Fwd: Confused by text and tail concepts of ElementTree
stefan_ml at behnel.de
Mon Aug 18 15:49:54 CEST 2008
Bogdan Bivolaru wrote:
> I again misposted my message to a member of the list, instead of the list
> itself. That is because I overuse the reply button and I don't recheck
> the address I am writing to.
It's actually sad that there are so many mailing lists out there that
break "reply" into "reply all except sender and CC".
Just use "reply all" when you reply to everyone and "reply" when you want
to reply to the sender, and then get used to fixing the address for
mailing lists that force your mail client into doing the wrong thing.
> I'm a little confused by the tail in the ElementTree Infoset:
> An example from that page: <ELEM key="value">text<SUBELEM />tail</ELEM>
> So this is the question:
> element.text = "text"
> element.tail = None
> element[:] = [<Element SUBELEM>]
> subelement.tag = "SUBELEM"
> subelement.tail = "tail"
> if element.text and subelement.tail are both children of element, why is
> tail actually attached to the subelement and not the element, as it's
Because that's where it sits in the document?
There are two ways to solve your confusion: a) think through the
implications, or b) get used to the way it works, as others have put
enough thoughts into it already.
> I expected that anything that refers an element is enclosed in the tags
> said element, instead I learn that a tag can contain reference to content
> that is outside it's tags: "<a/>aaa"
Where else would you expect a "tail" of something if not at the end of the
thing itself, and outside of its body?
> Doesn't this violate the concept of a tree of a single root element?
> Is it valid to reference root.tail?
Yes, the XML standard allows whitespace around the root element.
> In the particular case below:
> <ELEM key="value">text<SUBELEM />tail1<SUBELEM />tail2</ELEM>
> from what I understood I can access:
> text by "element.text"
> tail1 by "element.children.tail"
> tail2 by "element.children.tail"
The syntax for accessing the children of an element is "element[index]",
as with any indexable (or list-like) Python object. Just try it yourself,
More information about the XML-SIG