Conditioning xpath on presence of sibling , lxml



Here is a sample of an xml:



<w:p w:rsidR="00FE18D8" w:rsidRPr="00CF6C72" w:rsidRDefault="00FE18D8" w:rsidP="00FE18D8">
<w:pPr>
<w:pStyle w:val="BodyText"/>
<w:rPr>
<w:rFonts w:eastAsia="MS Mincho"/>
<w:b w:val="0"/>
<w:bCs w:val="0"/>
<w:sz w:val="16"/>
<w:lang w:eastAsia="ja-JP"/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00CF6C72">
<w:rPr>
<w:rFonts w:eastAsia="MS Mincho"/>
<w:b w:val="0"/>
<w:bCs w:val="0"/>
<w:sz w:val="16"/>
<w:lang w:eastAsia="ja-JP"/>
</w:rPr>
<w:t xml:space="preserve">Vol. </w:t>
</w:r>
</w:p>
<w:p w:rsidR="008A6DCF" w:rsidRPr="00CF6C72" w:rsidRDefault="005930C4">
<w:pPr>
<w:pStyle w:val="BodyText"/>
<w:jc w:val="center"/>
<w:rPr>
<w:bCs w:val="0"/>
<w:sz w:val="28"/>
<w:szCs w:val="28"/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00CF6C72">
<w:rPr>
<w:bCs w:val="0"/>
<w:sz w:val="28"/>
<w:szCs w:val="28"/>
</w:rPr>
<w:t xml:space="preserve">Adaptive </w:t>
</w:r>
</w:p>


and I needed to extract text corresponding to either or tags , but if both appear together , I need to skip the extraction.


In other words , extract text only if w:bCs is present or w:b.


What I've done is:



text2=" "
w = 'http://ift.tt/JiuBoE'
for r in p.xpath('.//w:t',namespaces={'w': w}):
if r.xpath('..//w:b|..//w:bCs[@w:val="0"]',namespaces={'w': w}):
text2 += r.text


This just checks whether w:b or w:bCs is present(and matches even if both are present). How could I add a condition for exclusivity?


No comments:

Post a Comment