A python port from java version located at: https://github.com/dept2/c14n2
Algorithm documentation page: https://www.w3.org/2008/xmlsec/Drafts/c14n-20/
Most of the test cases can be found on page: https://www.w3.org/2008/xmlsec/Drafts/c14n-20/test-cases/
- no additional packeges needed
- supports canonization with or without comment removal
- supports canonization with or without prefix rewriting
note: XML DTD mostly unsupported by python xml library, thus all the test cases with dtd involved are failed
$ git clone https://github.com/dept2/c14n2py
$ cd c14n2py
$ ./test.py
$ pip install https://github.com/dept2/c14n2py
from xml.dom.minidom import parseString
from c14n2 import DOMCanonicalizer, Parameters
body = parseString("""
<a:foo xmlns:a="http://a" xmlns:b="http://b" xmlns:c="http://c">
<b:bar/>
<b:bar/>
<b:bar/>
<a:bar b:att1="val"/>
</a:foo>""")
params = Parameters()
params.trimTextNodes = True
params.prefixRewrite = Parameters.SEQUENTIAL
c14n_body = DOMCanonicalizer.canonicalize(body, params)