$root = $xml->createElementNS('myNS', 'root');
rootmust not be in the namespace myNS. In the original example, it does not have a namespace.
$x = $xml->createElementNS('myNS', 'x', 'test');
p:x x, , p . , XML-- , p: .
, xmlns:p <p:x> (, ). , (-, XML-- ), setAttributeNS. :.
$root = $xml->createElementNS(null, 'root');
$xml->appendChild($root);
$x = $xml->createElementNS('myNS', 'p:x', 'test');
$root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:p', 'myNS');
$root->appendChild($x);