Search code examples
pythonxmllxmlc14n

lxml - TypeError: write() got an unexpected keyword argument 'default_namespace'


Below is a minimal working example. I have tested this with Python 3.4, Python 3.6 32 bit and Python 3.6 64 bit.

import io
from lxml import etree

test_node = etree.fromstring('''
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://docs.oasis-open.org/ws-sx/ws-trust/200512">
  <soap:Body>
     <ns1:RequestSecurityToken/>
  </soap:Body>
</soap:Envelope>''')
output = io.BytesIO(b'<?xml version="1.0" encoding="UTF-8"?>')
test_node.getroottree().write(output,
                         encoding="UTF-8",
                         xml_declaration=None,
                         default_namespace=None,
                         method="c14n",
                         short_empty_elements=False
                         )
output.seek(0)
print(output.read())

Result:

Traceback (most recent call last):
  File "C:/not_telling/c14n.py", line 16, in <module>
    short_empty_elements=False
  File "lxml.etree.pyx", line 1869, in lxml.etree._ElementTree.write (src\lxml\lxml.etree.c:57004)
TypeError: write() got an unexpected keyword argument 'short_empty_elements'

I have just upgraded lxml version to 4.0.0. (But the same is the issue with 3.7.)

I need to export with C14N method, and (although not included in the example) I also need to specify a list of namespaces that need to appear in the resulting canonical form. E.g. I must use the inclusive_ns_prefixes parameter too.

UPDATE: actually, it seems to be a problem with Python's built in xml module, not lxml.

Here is the method I'm calling:

https://github.com/python/cpython/blob/master/Lib/xml/etree/ElementTree.py#L721

It does have a short_empty_elements parameter, but it does not accept it.


Solution

  • The default_namespace and short_empty_elements parameters are not supported by the _ElementTree.write() method in lxml. See http://lxml.de/api/lxml.etree._ElementTree-class.html#write.

    However, both parameters are available in the ElementTree standard module since Python 3.4. See https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree.write.