Search code examples
pythongetcommentslxmlparent

Python lxml - find parent of comment


I need to find certain text from within a comment and then get the parent of that comment. here's the xml:

<video>
    <read_only_info>
        <read_only_value key="content-review-status">Live-Reviewed</read_only_value>
    </read_only_info>
    <title>Some Title</title>
    <assets>
        <asset type="full">
            <!—A place for some text—>
            <data_file role="source">
                <size>121190418069</size>
            </data_file>
        </asset>
        <asset type="preview">
            <!--A place for some text—>

So I need to find the text in the comment with "A Place for some text" then I need to get the parent of that comment, "full" or "preview".

I can find the comment with that text:

comments = tree.xpath('//comment()')
for c in comments:
    p = c.getparent()[0]
    print c.text

But don't know how to then get the parent of that text, "full" or "preview".


Solution

  • c.getparent() returns the parent element (the asset tag)

    >>> c.getparent()
    <Element asset at 0x103535d88>
    

    You can then call .attrib on that to access the attributes

    >>> c.getparent().attrib
    {'type': 'full'}
    >>> c.getparent().attrib['type']
    'full'