Convert XML to its C14N 2.0 serialised form. If *out* is provided, it must be a file or file-like object that receives the serialised canonical XML output (text, not bytes) through its ``.write()`` method. To write to a file, open it in text mode with encoding "utf-8". If *out* is
(xml_data=None, *, out=None, from_file=None, **options)
| 1759 | # C14N 2.0 |
| 1760 | |
| 1761 | def canonicalize(xml_data=None, *, out=None, from_file=None, **options): |
| 1762 | """Convert XML to its C14N 2.0 serialised form. |
| 1763 | |
| 1764 | If *out* is provided, it must be a file or file-like object that receives |
| 1765 | the serialised canonical XML output (text, not bytes) through its ``.write()`` |
| 1766 | method. To write to a file, open it in text mode with encoding "utf-8". |
| 1767 | If *out* is not provided, this function returns the output as text string. |
| 1768 | |
| 1769 | Either *xml_data* (an XML string) or *from_file* (a file path or |
| 1770 | file-like object) must be provided as input. |
| 1771 | |
| 1772 | The configuration options are the same as for the ``C14NWriterTarget``. |
| 1773 | """ |
| 1774 | if xml_data is None and from_file is None: |
| 1775 | raise ValueError("Either 'xml_data' or 'from_file' must be provided as input") |
| 1776 | sio = None |
| 1777 | if out is None: |
| 1778 | sio = out = io.StringIO() |
| 1779 | |
| 1780 | parser = XMLParser(target=C14NWriterTarget(out.write, **options)) |
| 1781 | |
| 1782 | if xml_data is not None: |
| 1783 | parser.feed(xml_data) |
| 1784 | parser.close() |
| 1785 | elif from_file is not None: |
| 1786 | parse(from_file, parser=parser) |
| 1787 | |
| 1788 | return sio.getvalue() if sio is not None else None |
| 1789 | |
| 1790 | |
| 1791 | _looks_like_prefix_name = re.compile(r'^\w+:\w+$', re.UNICODE).match |