2014-10-27 65 views
1

我有以下XML:提取文本w.r.t属性值,LXML的findall

<?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上的tutorials。我设法获取文本而不指定具有以下属性值:

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 

没有工作..

什么问题?

回答

1

只是在val太前面加{%(ns)s}

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 
1

我认为主要的问题不加区分val attirbute。但您也可以使用内置的.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