Извлечение текста по значению атрибута lxml findall

У меня есть следующий xml:

<?xml version="1.0" encoding="UTF-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
    <w:body>
        <w:p w:rsidR="00DD23CE" w:rsidRPr="00D1434D" w:rsidRDefault="009764DB" w:rsidP="00660FF7">
            <w:pPr>
                <w:pStyle w:val="HTMLPreformatted" />
                <w:spacing w:line="276" w:lineRule="auto" />
                <w:jc w:val="both" />
                <w:rPr>
                    <w:rFonts w:ascii="Times New Roman" w:eastAsia="MS PGothic" w:hAnsi="Times New Roman" />
                    <w:b />
                    <w:color w:val="000000" />
                    <w:sz w:val="24" />
                    <w:szCs w:val="24" />
                    <w:highlight w:val="yellow" />
                </w:rPr>
            </w:pPr>
            <w:bookmarkStart w:id="0" w:name="_GoBack" />
            <w:bookmarkEnd w:id="0" />
            <w:r w:rsidRPr="00D1434D">
                <w:rPr>
                    <w:rFonts w:ascii="Times New Roman" w:eastAsia="MS PGothic" w:hAnsi="Times New Roman" />
                    <w:b />
                    <w:color w:val="000000" />
                    <w:sz w:val="24" />
                    <w:szCs w:val="24" />
                    <w:highlight w:val="yellow" />
                </w:rPr>
                <w:t xml:space="preserve">Responses to </w:t>
            </w:r>
            <w:r w:rsidR="00335D4A" w:rsidRPr="00D1434D">
                <w:rPr>
                    <w:rFonts w:ascii="Times New Roman" w:eastAsia="MS PGothic" w:hAnsi="Times New Roman" />
                    <w:b />
                    <w:color w:val="000000" />
                    <w:sz w:val="24" />
                    <w:szCs w:val="24" />
                    <w:highlight w:val="yellow" />
                    <w:lang w:eastAsia="ja-JP" />
                </w:rPr>
                <w:t>the Reviewer</w:t>
            </w:r>
            <w:r w:rsidR="00335D4A" w:rsidRPr="00D1434D">
                <w:rPr>
                    <w:rFonts w:ascii="Times New Roman" w:eastAsia="MS PGothic" w:hAnsi="Times New Roman" />
                    <w:b />
                    <w:color w:val="000000" />
                    <w:sz w:val="24" />
                    <w:szCs w:val="24" />
                    <w:highlight w:val="yellow" />
                </w:rPr>
                <w:t xml:space="preserve">\xe2\x80\x99s </w:t>
            </w:r>
            <w:r w:rsidR="00DD23CE" w:rsidRPr="00D1434D">
                <w:rPr>
                    <w:rFonts w:ascii="Times New Roman" w:eastAsia="MS PGothic" w:hAnsi="Times New Roman" />
                    <w:b />
                    <w:color w:val="000000" />
                    <w:sz w:val="24" />
                    <w:szCs w:val="24" />
                    <w:highlight w:val="yellow" />
                </w:rPr>
                <w:t>Comments</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00200734" w:rsidRPr="00D1434D" w:rsidRDefault="00200734" w:rsidP="00660FF7">
            <w:pPr>
                <w:spacing w:line="276" w:lineRule="auto" />
                <w:rPr>
                    <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
                    <w:b />
                    <w:bCs />
                    <w:sz w:val="24" />
                    <w:szCs w:val="24" />
                    <w:highlight w:val="yellow" />
                </w:rPr>
            </w:pPr>
        </w:p>
        <w:p w:rsidR="00675BBA" w:rsidRPr="00D1434D" w:rsidRDefault="00675BBA" w:rsidP="00660FF7">
            <w:pPr>
                <w:spacing w:line="276" w:lineRule="auto" />
                <w:rPr>
                    <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
                    <w:b />
                    <w:bCs />
                    <w:sz w:val="24" />
                    <w:szCs w:val="24" />
                    <w:highlight w:val="yellow" />
                </w:rPr>
            </w:pPr>
            <w:r w:rsidRPr="00D1434D">
                <w:rPr>
                    <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
                    <w:b />
                    <w:bCs />
                    <w:sz w:val="24" />
                    <w:szCs w:val="24" />
                    <w:highlight w:val="yellow" />
                </w:rPr>
                <w:t>Comments of Reviewer 1</w:t>
            </w:r>
        </w:p>
    </w:body>
</w:document>

Мне нужно извлечь текст с тегом w:highlight и значением его атрибута как yellow. Я имел в виду руководства по xml. Мне удалось получить текст без указания значения атрибута со следующим:

w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
for p in lxml_tree.findall('.//{' + w + '}p'):
    for t in p.findall('.//{%(ns)s}highlight/../..//{%(ns)s}t' %{'ns':w}):
        print t.text  

но он ничего не возвращает, когда я указываю значение атрибута:

w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
for p in lxml_tree.findall('.//{' + w + '}p'):
    for t in p.findall('.//{%(ns)s}highlight[@val="yellow"]/../..//{%(ns)s}t' %{'ns':w}):
        print t.text  

Даже используя:

for p in lxml_tree.findall('.//{' + w + '}p'):
        for t in p.findall('.//{%(ns)s}*[@highlight="yellow"]/../..//{%(ns)s}t' %{'ns':w}):
            print t.text  

не сработало..

В чем проблема??


person Hypothetical Ninja    schedule 27.10.2014    source источник


Ответы (2)


Просто добавьте {%(ns)s} перед val тоже:

for p in lxml_tree.findall('.//{' + w + '}p'):
    for t in p.findall('.//{%(ns)s}highlight[@{%(ns)s}val="yellow"]/../..//{%(ns)s}t' %{'ns':w}):
        print t.text
person miles82    schedule 30.10.2014

Я думаю, что основная проблема не в пространстве имен атрибута val. Но вы также можете немного упростить код, используя встроенный метод .xpath(), например:

for t in lxml_tree.xpath('.//w:highlight[@w:val="yellow"]/../..//w:t',
    namespaces={'w': "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
}):
    print t.text
person Michael Schuller    schedule 30.10.2014