2

Can I use element.findall() from xml.etree.ElementTree to find an element with multiple values in a single attribute?

I can use this to find all of the div elements with class="day":

from xml.etree.ElementTree import ElementTree

calendar = ElementTree()
calendar.parse("calendar-week.xml")

calendar.findall('.//div[@class="day"]')

or this to find all of the div elements with the class="first day":

calendar.findall('.//div[@class="first day"]')

but is there way to find all of the div elements with "day" in their class? I can not find any docs on how to do this. Is there a way to use regular expressions here?

<body>
   <div class="calendar-week">
      <div class="first day" id="su">Sunday</div>
      <div class="day" id="mo">Monday</div>
      <div class="day" id="tu">Tuesday</div>
      <div class="day" id="we">Wednesday</div>
      <div class="day" id="th">Thursday</div>
      <div class="day" id="fr">Friday</div>
      <div class="last day" id="sa">Saturday</div>
      <div class="clear-float"></div>
   </div><!-- /calendar-week -->
</body>    
dansalmo
  • 11,506
  • 5
  • 58
  • 53
  • Found [this](http://stackoverflow.com/questions/1390568/xpath-how-to-match-attributes-that-contain-a-certain-string) with more examples like one from @stranac but none of them work with xml.etree.ElementTree. Maybe xml.etree does not support all xpath options? – dansalmo Jul 05 '12 at 21:47

2 Answers2

1

This xpath is how you would usually do this:

'//div[contains(concat(" ", @class, " "), " day ")]'
stranac
  • 26,638
  • 5
  • 25
  • 30
1

This code worked based on the xpath term provided in the answer from @stranac

from lxml import etree as ET

calendar = ET.ElementTree()
calendar.parse("calendar-week.xml")

elem_list = calendar.xpath('//div[contains(concat(" ", @class, " "), " day ")]')
dansalmo
  • 11,506
  • 5
  • 58
  • 53