Reputation: 16
I try to parse an XML file with lxml. It works with some files but the last one gave me an error:
XPathEvalError at /admin/xml_exportation/xml/add/
Invalid expression
Here is the xml code :
<?xml version="1.0" encoding="utf-8" ?>
<export source="Source" version="1.0" date="2014-02-21T17:46:57">
<Agence ID="XX1" externRef="XX1" customCode="38495">
<Biens>
<Bien ID="XX1-176" ref="XX1-176">
<DateMAJ>2013-06-14T12:12:07</DateMAJ>
</Bien>
</Biens>
</Agence>
</export>
And then the python code :
tree = etree.parse(xml_file)
root_path = '/export/Agence/Biens/Bien/'
for element in tree.xpath(root_path):
print(element)
I tried to change the path but it gave me the same error.
Thank you for your help.
Internal Server Error: /admin/xml_exportation/xml/add/
Traceback (most recent call last):
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 372, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/views/decorators/cache.py", line 89, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 202, in inner
return view(request, *args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/utils/decorators.py", line 25, in _wrapper
return bound_func(*args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/utils/decorators.py", line 21, in bound_func
return func(self, *args2, **kwargs2)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/db/transaction.py", line 223, in inner
return func(*args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1007, in add_view
self.save_model(request, new_object, form, False)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 740, in save_model
obj.save()
File "astucesexperts/apps/xml_exportation/models.py", line 154, in save
for element in tree.xpath(root_path): # Find each new property
File "lxml.etree.pyx", line 2115, in lxml.etree._ElementTree.xpath (src/lxml/lxml.etree.c:57669)
File "xpath.pxi", line 370, in lxml.etree.XPathDocumentEvaluator.__call__ (src/lxml/lxml.etree.c:146579)
File "xpath.pxi", line 238, in lxml.etree._XPathEvaluatorBase._handle_result (src/lxml/lxml.etree.c:144977)
File "xpath.pxi", line 224, in lxml.etree._XPathEvaluatorBase._raise_eval_error (src/lxml/lxml.etree.c:144832)
XPathEvalError: Invalid expression
I also add the code from an xml file I could export with the script (changing the root path).
<?xml version='1.0' encoding='UTF-8' ?>
<ANNONCES>
<ANNONCE>
<AGENCE_REF><![CDATA[22]]></AGENCE_REF>
<AGENCE_NOM><![CDATA[Agence]]></AGENCE_NOM>
<REFERENCE><![CDATA[100lm]]></REFERENCE>
<TRANSACTION><![CDATA[2]]></TRANSACTION>
<TYPE><![CDATA[2]]></TYPE>
<SOUSTYPE><![CDATA[45]]></SOUSTYPE>
<SURFACE><![CDATA[100]]></SURFACE>
<ANNONCE>
</ANNONCES>
Upvotes: 0
Views: 1490
Reputation: 73440
Your XPath has a trailing '/'. Remove it and everything works as expected.
That is, change:
root_path = '/export/Agence/Biens/Bien/'
to
root_path = '/export/Agence/Biens/Bien'
Upvotes: 1
Reputation: 2005
Your closing tag for <Biens>
has no backslash, so your tree is empty because XML did not parse.
Validate your XML before you try parsing it in Python.
Upvotes: 0