1
votes

the following does not appear to parse the xml data correctly or is doing something wrong.

This is the code being run.

from lxml import etree from lxml.etree import fromstring

if request.POST:

    xml = request.POST['xml'].encode('utf-8')
    parser = etree.XMLParser(ns_clean=True, recover=True, encoding='utf-8')
    h = fromstring(xml, parser=parser)
    status = h.cssselect('itagg_delivery_receipt status').text_content()
    return HttpResponse(status)

The error:

AttributeError: 'lxml.etree._Element' object has no attribute 'cssselect' status

This is the xml document that is being sent:

 <?xml version="1.1" encoding="ISO-8859-1"?>
<itagg_delivery_receipt>
<version>1.0</version>
<msisdn>447889000000</msisdn>
<submission_ref>
845tgrgsehg394g3hdfhhh56445y7ts6</
submission_ref>
<status>Delivered</status>
<reason>4</reason>
<timestamp>20050709120945</timestamp>
<retry>0</retry>
</itagg_delivery_receipt> 

I have dumped out str(h) and it looks like this <element 0x7fd341e93870="" at="" itagg_delivery_receipt=""></element>

2
i don't think you can use cssselect on an xml doc. If you import from lxml.html you get cssselect, but i don't know if that's appropriate for your XML file. - leech

2 Answers

1
votes

Here's how to get the thing you want with xpath:

>>> h.xpath('status/text()')
['Delivered']

So with a little helper function in your code:

def first(seq,default=None):
  for item in seq:
    return item
  return default

...

status = first(h.xpath('status/text()'))
0
votes

Before using cssselect you need to install it

pip install cssselect