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 a tag w:highlight

that has attribute value

= "yellow" . I searched for it but couldn't find a solution.

The following works to 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"]'):

      

it doesn't work, returns nothing.

+3


source to share


1 answer


w:val

the attribute is in a namespace, so you cannot just address it @val

. One possible solution is to use an expression @*[local-name()='attribute name']

to address with the local attribute name, similar to what you did for elements:



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

      

+8


source







All Articles