diff --git a/Doc/library/xml.dom.minidom.rst b/Doc/library/xml.dom.minidom.rst
index 39c41cb9a30317..d9b7bc7356e059 100644
--- a/Doc/library/xml.dom.minidom.rst
+++ b/Doc/library/xml.dom.minidom.rst
@@ -157,6 +157,7 @@ module documentation. This section lists the differences between the API and
.. versionchanged:: next
Namespace declarations missing for the serialized element
and its attributes are now written.
+ It now works for :class:`!DocumentFragment` nodes.
.. method:: Node.toxml(encoding=None, standalone=None)
@@ -179,6 +180,9 @@ module documentation. This section lists the differences between the API and
.. versionchanged:: 3.9
The *standalone* parameter was added.
+ .. versionchanged:: next
+ It now works for :class:`!DocumentFragment` nodes.
+
.. method:: Node.toprettyxml(indent="\t", newl="\n", encoding=None, \
standalone=None)
@@ -207,6 +211,7 @@ module documentation. This section lists the differences between the API and
.. versionchanged:: next
Whitespace is no longer added inside an element with mixed content
or marked with ``xml:space="preserve"``.
+ It now works for :class:`!DocumentFragment` nodes.
.. _dom-example:
diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst
index aa875362c9b628..8e1dd93f6ed873 100644
--- a/Doc/whatsnew/3.16.rst
+++ b/Doc/whatsnew/3.16.rst
@@ -725,6 +725,12 @@ xml
rather than defaulted from the DTD.
(Contributed by Jason Orendorff and Serhiy Storchaka in :gh:`44871`.)
+* The :meth:`~xml.dom.minidom.Node.writexml`,
+ :meth:`~xml.dom.minidom.Node.toxml` and
+ :meth:`~xml.dom.minidom.Node.toprettyxml` methods
+ now work for :class:`!DocumentFragment` nodes in :mod:`xml.dom.minidom`.
+ (Contributed by Serhiy Storchaka in :gh:`54092`.)
+
* :class:`~xml.etree.ElementTree.XMLPullParser` and
:func:`~xml.etree.ElementTree.iterparse` now support the *target* parameter.
The reported object is the value returned by the corresponding method of
diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py
index 37829eaef0e5d4..446bbe096bd19d 100644
--- a/Lib/test/test_minidom.py
+++ b/Lib/test/test_minidom.py
@@ -571,6 +571,21 @@ def testWriteXML(self):
dom.unlink()
self.assertEqual(str, domstr)
+ def testWriteXMLDocumentFragment(self):
+ dom = parseString('text')
+ frag = dom.createDocumentFragment()
+ for node in list(dom.documentElement.childNodes):
+ frag.appendChild(node)
+ self.assertEqual(frag.toxml(), 'text')
+ self.assertEqual(frag.toprettyxml(),
+ '\ntext\n\n')
+ # the fragment itself does not add a level of indentation
+ writer = io.StringIO()
+ frag.writexml(writer, " ", " ", "\n")
+ self.assertEqual(writer.getvalue(),
+ ' \n text\n \n')
+ self.assertEqual(dom.createDocumentFragment().toxml(), '')
+
def testWriteXMLNamespaceDeclarations(self):
dom = Document()
root = dom.appendChild(
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py
index d62564da3f36c6..7cb652a323dcc2 100644
--- a/Lib/xml/dom/minidom.py
+++ b/Lib/xml/dom/minidom.py
@@ -515,6 +515,10 @@ class DocumentFragment(Node):
def __init__(self):
self.childNodes = NodeList()
+ def writexml(self, writer, indent="", addindent="", newl=""):
+ for node in self.childNodes:
+ node.writexml(writer, indent, addindent, newl)
+
class Attr(Node):
__slots__=('_name', '_value', 'namespaceURI',
diff --git a/Misc/NEWS.d/next/Library/2026-08-30-13-37-47.gh-issue-54092.Tq8Wm3.rst b/Misc/NEWS.d/next/Library/2026-08-30-13-37-47.gh-issue-54092.Tq8Wm3.rst
new file mode 100644
index 00000000000000..43ae25552841b1
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-08-30-13-37-47.gh-issue-54092.Tq8Wm3.rst
@@ -0,0 +1,6 @@
+Implement :meth:`~xml.dom.minidom.Node.writexml` for document fragments
+in :mod:`xml.dom.minidom`,
+so that :meth:`~xml.dom.minidom.Node.toxml` and
+:meth:`~xml.dom.minidom.Node.toprettyxml` now work for them.
+They write the children of the fragment,
+without adding a level of indentation.