【发布时间】:2014-12-02 19:23:39
【问题描述】:
我正在查看 DOMDocument::createElementNS 文档:http://php.net/manual/en/domdocument.createelementns.php
它说第二个变量“qualifiedName”必须定义为前缀:标记名,但我发现在某些情况下前缀是自动添加的(我没有在代码中输入它)。我做了一个例子:
<?php
//Namespaces url
$NS_xx = 'http://xxx';
$NS_yy = 'http://yyy';
$domxml = new DomDocument('1.0', 'UTF-8');
$Country = $domxml->appendChild ($domxml->createElementNS($NS_xx, 'xx:Country')); // Manually entered prefix
$Country->setAttributeNS($NS_xx, 'id', '1'); // Automatically added prefix in result
$State = $Country->appendChild ($domxml->createElementNS($NS_xx,'State')); // Automatically added prefix in result
$Region = $State->appendChild ($domxml->createElementNS($NS_yy, 'yy:Region')); // Manually entered prefix
$Region->setAttributeNS($NS_xx, 'id', '5'); // Automatically added prefix in result
$Town = $Region->appendChild ($domxml->createElement('Town'));
$Town->appendChild ($domxml->createElementNS($NS_yy, 'F', 'New York')); // Automatically added prefix in result
$Town->setAttributeNS($NS_xx, 'zip', '10001'); // Automatically added prefix in result
Header('Content-type: text/xml');
$domxml->formatOutput = true;
echo $domxml->saveXML();
?>
它回馈:
<?xml version="1.0" encoding="UTF-8"?>
<xx:Country xmlns:xx="http://xxx" xx:id="1">
<xx:State>
<yy:Region xmlns:yy="http://yyy" xx:id="5">
<Town xx:zip="10001">
<yy:F>New York</yy:F>
</Town>
</yy:Region>
</xx:State>
</xx:Country>
在我看来,如果之前在任何父元素中添加了前缀,它将自动添加。是否有任何理由每次在代码中添加该前缀? 如果我按照文档所述在代码中手动添加这些前缀,则结果 xml 将是相同的...
【问题讨论】:
标签: php dom xml-namespaces