!

Dette materialet blir ikke lenger vedlikeholdt. Du vil finne oppdatert materiale på siden: http://borres.hiof.no/wep/

lxml
XPATH
Børre Stenseth
Python > lxml >XPath

XPath

Hva
Søke med XPath

lxml implementerr 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='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='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='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='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')
Referanser
  1. lxml - XML and HTML with Python lxml.de/ 03-08-2011
Vedlikehold
Børre Stenseth, august 2011
( Velkommen ) Python > lxml >XPath ( XSLT )