More options for XML canonicalization in write_c14n

Bug #253677 reported by Philippe Lagadec
2
Affects Status Importance Assigned to Milestone
lxml
Fix Released
Wishlist
Unassigned

Bug Description

XML Canonicalization (C14N) is useful in some cases such as digital signature. lxml provides a very easy way to do it in Python. However, the current version lxml 2.1 does not give access to all C14N parameters. This is a simple patch to improve its C14N support.

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()

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
However, the options are not exposed in the lxml write_c14n method. Current versions of lxml provide only inclusive C14N with comments, which may not always be the right solution.

It is possible to fix that limitation by changing a few lines in lxml source code, and recompiling it.

Attached are patched versions of lxml.etree.pyx and serializer.pxi, based on the latest SVN version.

Here is how to use this patched version:

import lxml.etree as ET
et = ET.parse('file.xml')
output = StringIO.StringIO()
et.write_c14n(output, exclusive=1, with_comments=0)
print output.getvalue()

As the default values are the same as in lxml 2.1, this patch should not have any impact on current code using lxml.

For more info, see http://decalage.info/drupal/en/python/lxml-c14n

Revision history for this message
Philippe Lagadec (decalage) wrote :
Revision history for this message
scoder (scoder) wrote : Re: [Bug 253677] [NEW] More options for XML canonicalization in write_c14n

Thanks for the changes, they will be in lxml 2.2.

It's common to send patches, though. If you are using SVN anyway, "svn diff"
will do the right thing.

scoder (scoder)
Changed in lxml:
importance: Undecided → Wishlist
status: New → Fix Committed
scoder (scoder)
Changed in lxml:
status: Fix Committed → Fix Released
scoder (scoder)
Changed in lxml:
milestone: none → 2.2
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.