Php 如何使用为自身指定名称空间的节点生成xml?

Php 如何使用为自身指定名称空间的节点生成xml?,php,xml,dom,domdocument,Php,Xml,Dom,Domdocument,这是我想要的结果: <?xml version="1.0" encoding="utf-8"?> <types> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"/> </types> 我只知道: <?xml version="1.0" encoding="utf-8"?> <types xmlns:xs="http://www.w3.org/2001/XMLS

这是我想要的结果:

<?xml version="1.0" encoding="utf-8"?>
<types>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
</types>
我只知道:

<?xml version="1.0" encoding="utf-8"?>
<types xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
</types>


我遗漏了什么?

问题在于附加子项的顺序。试试这个:

$document = new DOMDocument('1.0', 'utf-8');

$types = $document->createElement('types');
$schema = $document->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:schema');

$document->appendChild($types);
$types->appendChild($schema);

echo $document->saveXML();

问题是按附加子项的顺序。试试这个:

$document = new DOMDocument('1.0', 'utf-8');

$types = $document->createElement('types');
$schema = $document->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:schema');

$document->appendChild($types);
$types->appendChild($schema);

echo $document->saveXML();

你怎么知道的?这背后有什么逻辑或合理的解释吗?可怕的是,这是一种简化,我必须按问题中的顺序添加它们:-(@zerkms我需要重新阅读文档,为你的评论中的问题提供准确的答案。你是怎么知道的?这背后有什么逻辑或任何合理的解释吗?可怕的是,这是一种简化,我必须按照问题中的顺序添加它们:-(@zerkms我需要重新阅读文档,以便为您评论中的问题提供准确答案