diff --git a/lib/SAML2/XML/md/EntitiesDescriptor.php b/lib/SAML2/XML/md/EntitiesDescriptor.php
index a6b66b88b520f1cfe2ba49ffaa26b5a3bbcc4587..e3ea236dc6785c19c36ade48ec707278c7a7a73f 100644
--- a/lib/SAML2/XML/md/EntitiesDescriptor.php
+++ b/lib/SAML2/XML/md/EntitiesDescriptor.php
@@ -110,6 +110,7 @@ class SAML2_XML_md_EntitiesDescriptor extends SAML2_SignedElementHelper {
 		if ($parent === NULL) {
 			$doc = new DOMDocument();
 			$e = $doc->createElementNS(SAML2_Const::NS_MD, 'md:EntitiesDescriptor');
+			$doc->appendChild($e);
 		} else {
 			$e = $parent->ownerDocument->createElementNS(SAML2_Const::NS_MD, 'md:EntitiesDescriptor');
 			$parent->appendChild($e);
diff --git a/lib/SAML2/XML/md/EntityDescriptor.php b/lib/SAML2/XML/md/EntityDescriptor.php
index bd35f144ee545dbd5335e276eece343c93af65e7..89c7dcefc8ab082c24248195a46ced036b70681e 100644
--- a/lib/SAML2/XML/md/EntityDescriptor.php
+++ b/lib/SAML2/XML/md/EntityDescriptor.php
@@ -202,6 +202,7 @@ class SAML2_XML_md_EntityDescriptor extends SAML2_SignedElementHelper {
 		if ($parent === NULL) {
 			$doc = new DOMDocument();
 			$e = $doc->createElementNS(SAML2_Const::NS_MD, 'md:EntityDescriptor');
+			$doc->appendChild($e);
 		} else {
 			$e = $parent->ownerDocument->createElementNS(SAML2_Const::NS_MD, 'md:EntityDescriptor');
 			$parent->appendChild($e);