XML 语法属性 PHP - 键问题


XML Syntax attributes PHP - issue with key

>i 使用 PHP 创建一个 XML 文件

$xml=new SimpleXMLElement('<config/>');
$xml->addAttribute("xmlns","http://www.toto.com/tot_config_20110606");
$xml->addAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
$xml->addAttribute("xsi:schemaLocation","http://www.toto.com/tot_config_20110606 config.xsd"); 
   //some childrens...
file_put_contents($filename, $xml->asXML() , LOCK_EX);

结果是一个正确的XML文件,但我对属性有问题

结果是:

<config xmlns="http://www.toto.com/tot_config_20110606" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.toto.com/tot_config_20110606 config.xsd">
   //...
</config>

有人可以解释我如何保留整个密钥吗?

但是我需要属性的第一部分结果:

<config  xmlns="http://www.toto.com/tot_config_20110606" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.toto.com/tot_config_20110606 config.xsd">
   //...
</config>

尝试一个 p.servus 在这里提出的建议,并将父架构的 URL 添加为addAttribute()的第三个参数:

$xml = new SimpleXMLElement("<config></config>");
$xml->addAttribute("xmlns","http://www.toto.com/tot_config_20110606");
/***** Update: the following line must be deleted****/
// $xml->addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance", "http://www.toto.com/tot_config_20110606"); 
$xml->addAttribute("xsi:schemaLocation", "http://www.toto.com/tot_config_20110606 config.xsd", "http://www.w3.org/2001/XMLSchema-instance"); 
echo $xml->asXml();