XPath
De aktuelle dataene er ordnet i en XML-fil: all_results.xml
Kopier datafila og pythonkoden nedenfor og eksperimenter.
Eksempel
""" working with XPATH """ from lxml import etree """ all athlets on 100m in all olympics """ def testing1(): xmlfile='../lxml/all_results.xml' tree=etree.parse(xmlfile) rot=tree.getroot() print ("-------- 100m athlets ------------") names= rot.xpath("//event[@dist='100m']/athlet/name") for n in names: print (n.text) """ all athlets on 100m in all olympics grouped on place """ def testing2(): xmlfile='../lxml/all_results.xml' tree=etree.parse(xmlfile) rot=tree.getroot() print ("-------- 100m athlets ------------") events= rot.xpath("//event[@dist='100m']") for e in events: # parent .. print (e.xpath("../@place")[0]) athlets=e.xpath("athlet") for a in athlets: print ("\t\t"+a.xpath("name")[0].text) """ all athlets on a named distance in all olympics grouped on place """ def testing3(distance): xmlfile='../lxml/all_results.xml' tree=etree.parse(xmlfile) rot=tree.getroot() print ("-------- %s athlets ------------"%distance) xp="//event[@dist='%s']"%distance events= rot.xpath(xp) for e in events: # parent .. print (e.xpath("../@place")[0]) athlets=e.xpath("athlet") for a in athlets: print ("\t\t"+a.xpath("name")[0].text) """ all athlets on a named distance in a named olympics """ def testing4(place,distance): xmlfile='../lxml/all_results.xml' tree=etree.parse(xmlfile) rot=tree.getroot() print ("-------- %s athlets in %s------------"%(distance,place)) xp="//OlympicGame[@place='%s']/event[@dist='%s']/athlet/name"%(place,distance) names= rot.xpath(xp) for n in names: print (n.text) testing1() testing2() testing3('400m') testing4('Beijing','200m')