
On Thu, 21 Apr 2011 08:26:24 -0400 Mag Gam <magawake@gmail.com> wrote:
I am in search for a good xpath example.
Let say I have, this xml file
<?xml version="1.0"?> <catalog> <book id="bk101"> <genre><s>Computer</s></genre> <price><f>44.95</f></price> <publish_date><d>2000-10-01</d></publish_date> <description><s>An in-depth look at creating applications with XML.</s></description> </book> </catalog>
How can I extract only the price of a book which has a genre of "computers"? How can I extract, price, description of book id"bk101" ?
http://www.w3.org/TR/xpath/ http://www.w3schools.com/xpath/default.asp from lxml import etree dom = etree.parse('d.xml') p = dom.xpath('//book[genre/s="Computer"]/price/f/text()')[0] bk = dom.xpath('//book[@id="bk101"]')[0] p2 = bk.xpath('.//price/f/text()')[0] d = bk.xpath('.//description/s/text()')[0] print p, p2, d It might be better to do something like d = ' '.join(bk.xpath('.//description//text()')) to collect all the text under the description node, depends whether there's ever more than one <s/> etc. Cheers -Terry
________________________________________________________________ Mailing list for the lxml Python XML toolkit - http://lxml.de/ lxml@lxml.de http://mailman-mail5.webfaction.com/listinfo/lxml