Reputation: 4077
I have the following 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>
I need to extract text with the w:highlight
tag and its attribute value as yellow
. I was referring to the tutorials on xml. I have managed to get the text without specifying the attribute value with the following:
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
but it doesn't return anything when I specify the attribute value:
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
Even using:
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
did not work..
What is the problem??
Upvotes: 1
Views: 211
Reputation: 494
I think the main problem is not namespacing the val
attirbute. But you can also simplify the code a bit using the built-in .xpath()
method, like so:
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
Upvotes: 1
Reputation: 6794
Just add {%(ns)s}
in front of val
too:
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
Upvotes: 1