Hypothetical Ninja
Hypothetical Ninja

Reputation: 4077

Attributes in Xpath local-name()

This is a small sample of my xml file.

<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
   <w:pPr>
      <w:rPr>
         <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:p> 

I want to extract text with the w:highlight tag specifically having the attribute value = "yellow" . I searched for it but wasn't able to come up with a solution.

The following works for highlight in general:

for t in source.xpath('.//*[local-name()="highlight"]/../..//*[local-name()="t"]'):
     do something  

I tried :

for t in lxml_tree.xpath('//*[local-name()="highlight"][@val="yellow"]/../..//*[local-name()="t"]'):

this doesn't work, returns nothing..

Upvotes: 8

Views: 17239

Answers (1)

har07
har07

Reputation: 89295

w:val attribute is in namespace, so you can't just address it by @val. One possible solution is by using @*[local-name()='attribute name'] expression to address an attribute by it's local name, similar to what you've done for elements :

//*[local-name()="highlight"][@*[local-name()='val' and .='yellow']]/../..//*[local-name()="t"]

Upvotes: 19

Related Questions