Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Doc/library/xml.etree.elementtree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,26 @@ These two approaches both output::
|--> Gunther
|--> Commander Clement

Serializing XML with Default Namespace
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. _elementtree-xpath:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the target for the following section

When serializing XML that contains a default namespace, ElementTree will by
default add a generated prefix (such as ``ns0``) instead of preserving the
default namespace. For example::
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t agree with the phrasing here! (after instead of)


>>> root = ET.fromstring("<doc xmlns='http://example.com'><p>text</p></doc>")
>>> print(ET.tostring(root, encoding='unicode'))
<ns0:doc xmlns:ns0="http://example.com"><ns0:p>text</ns0:p></ns0:doc>

To preserve the default namespace during serialization, use the
*default_namespace* parameter with :func:`tostring` or :meth:`ElementTree.write`::

>>> root = ET.fromstring("<doc xmlns='http://example.com'><p>text</p></doc>")
>>> print(ET.tostring(root, encoding='unicode', default_namespace='http://example.com'))
<doc xmlns="http://example.com"><p>text</p></doc>

.. versionadded:: 3.8
The *default_namespace* parameter.

XPath support
-------------
Expand Down
Loading