diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index 3e4ca874d8..9eddafde01 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -757,7 +757,7 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent)
 
 			/* This attaches root to doc, so we need not free it separately. */
 			xmlDocSetRootElement(doc, root);
-			xmlAddChild(root, content_nodes);
+			xmlAddChildList(root, content_nodes);
 
 			/*
 			 * We use this node to insert newlines in the dump.  Note: in at
@@ -1842,10 +1842,24 @@ xml_parse(text *data, XmlOptionType xmloption_arg,
 													   parsed_nodes);
 				if (res_code != 0 || xmlerrcxt->err_occurred)
 				{
-					xml_errsave(escontext, xmlerrcxt,
-								ERRCODE_INVALID_XML_CONTENT,
-								"invalid XML content");
-					goto fail;
+#if LIBXML_VERSION >= 21300
+					/*
+					 * libxml2 2.13 reports namespace errors at this point via
+					 * res_code instead of xmlerrcxt.  Ignore them here so that
+					 * we can report the same error message as for libxml2
+					 * releases prior to 2.13.
+					 */
+					if (res_code != XML_NS_ERR_UNDEFINED_NAMESPACE &&
+						res_code != XML_WAR_NS_URI)
+					{
+#endif
+						xml_errsave(escontext, xmlerrcxt,
+									ERRCODE_INVALID_XML_CONTENT,
+									"invalid XML content");
+						goto fail;
+#if LIBXML_VERSION >= 21300
+					}
+#endif
 				}
 			}
 		}
