diff --git a/Doc/library/xml.dom.minidom.rst b/Doc/library/xml.dom.minidom.rst
index efc81f31e36a5b..cbecbda4839ad8 100644
--- a/Doc/library/xml.dom.minidom.rst
+++ b/Doc/library/xml.dom.minidom.rst
@@ -154,6 +154,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.toxml(encoding=None, standalone=None)
Return a string or byte string containing the XML represented by
@@ -175,6 +178,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)
@@ -194,6 +200,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.
+
.. _dom-example:
DOM Example
diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst
index f3ddae7a2b2fdd..909895957bd1f9 100644
--- a/Doc/whatsnew/3.16.rst
+++ b/Doc/whatsnew/3.16.rst
@@ -648,6 +648,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`.)
+
zipfile
-------
diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py
index 3735a6046891ea..99bf0132855dad 100644
--- a/Lib/test/test_minidom.py
+++ b/Lib/test/test_minidom.py
@@ -561,6 +561,22 @@ 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(), '')
+ dom.unlink()
+
def test_toxml_quote_text(self):
dom = Document()
elem = dom.appendChild(dom.createElement('elem'))
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py
index 5fd3911bd3c9eb..a1b544fc4585cf 100644
--- a/Lib/xml/dom/minidom.py
+++ b/Lib/xml/dom/minidom.py
@@ -379,6 +379,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.