This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author scoder
Recipients eli.bendersky, martin.panter, rhettinger, scoder
Date 2014-03-23.07:34:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1395560061.72.0.74987657837.issue21028@psf.upfronthosting.co.za>
In-reply-to
Content
catalog = xml.etree.ElementTree.parse('books.xml')

    # This succeeds
    for book in catalog.findall('book'):
        print(book.tag)

This is a bit of a convenience break in the API. The "normal" way to do it would be either catalog.getroot().findall('book') or catalog.findall('/catalog/book'). There is not much use in requiring to include the root element in the path expression, therefore it's allowed to leave it out. Note that you can't use absolute path expressions on Elements, this is a difference to the ElementTree object.


    # This fails:
    for book in catalog:
        print(book.tag)

Iterating over an ElementTree? What would that even mean? Why would you expect it to iterate over the children of the root Element, and not, say, all Elements in the document? I think that ambiguity is a good reason to not make ElementTree objects iterable.


    # But for inner elements, we have more options
    book = catalog.find('bk101')
    for subelement in book:
        print(subelement.tag)


> Note, the XML data model requires that the outermost element have some capabilities that inner elements don't have (such as comments and processing instructions).  That said, the outer element shouldn't have fewer capabilities that the inner elements.

ISTM that you are misinterpreting the ElementTree object as representing the document root whereas it actually represents the document. The root Element is given by tree.getroot().
History
Date User Action Args
2014-03-23 07:34:21scodersetrecipients: + scoder, rhettinger, eli.bendersky, martin.panter
2014-03-23 07:34:21scodersetmessageid: <1395560061.72.0.74987657837.issue21028@psf.upfronthosting.co.za>
2014-03-23 07:34:21scoderlinkissue21028 messages
2014-03-23 07:34:21scodercreate