Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Exclusive XML Canonicalization (xml-exc-c14n)

Tags:

python

xml

In Python, I need to Canonicalize (c14n) an XML string.

Which module/package can I use for this? And how should I do this?

(I prefer to use default python 2.7 modules, without extra installs or patches.)

For reference see: http://www.w3.org/TR/xml-exc-c14n/

like image 338
taper Avatar asked Feb 14 '23 06:02

taper


1 Answers

from http://www.decalage.info/en/python/lxml-c14n

lxml provides a very easy way to do c14n in python. <..>

Here is an example showing how to perform C14N using lxml 2.1:

import lxml.etree as ET
et = ET.parse('file.xml')
output = StringIO.StringIO()
et.write_c14n(output)
print output.getvalue()

from lxml docs:

write_c14n(self, file, exclusive=False, with_comments=True, compression=0, inclusive_ns_prefixes=None)

C14N write of document. Always writes UTF-8.

<..>

Also there is libxml2:

XML C14N version 1.0 provides two options which make four possibilities (see http://www.w3.org/TR/xml-c14n and http://www.w3.org/TR/xml-exc-c14n/):

  • Inclusive or Exclusive C14N
  • With or without comments

libxml2 gives access to these options in its C14N API: http://xmlsoft.org/html/libxml-c14n.html

Though obligatory check for version changes in these two libs.

like image 150
Bruno Gelb Avatar answered Feb 15 '23 23:02

Bruno Gelb