$doc = new DOMDocument(); $root = $doc->createElementNS('http://example.com/ns', 'root'); $doc->appendChild($root); $child = $doc->createElementNS('http://example.com/ns', 'child'); $root->appendChild($child); $child->setAttributeNS('http://example.com/ns', 'foo:bar', 'baz');
$doc = new DOMDocument(); $root = $doc->createElement('root'); $doc->appendChild($root); $child = $doc->createElement('child'); $root->appendChild($child); $child->setAttributeNS('http://example.com/ns', 'foo:bar', 'baz');In this example, we create an XML document without any namespace information. We create a root element and append it to the document. We also create a child element and append it to the root. Finally, we set an attribute named 'foo:bar' with the value 'baz' on the child element, using the namespace URI 'http://example.com/ns'. Package/Library: PHP Standard Library (php-xml)