希望有人能够解释为什么会发生这种情况。
我在代码中的其他地方使用了完全相同的语法,没有任何问题,但由于某种原因,这不起作用:
$sale = $xml->addChild('sale', null, 'http://www.capita-software-services.com/scp/simple');
在我的代码中,我有以下内容:
$sale = $xml->addChild('sale', null, 'http://www.capita-software-services.com/scp/simple');
$sale->addChild('saleSummary', null, 'http://www.capita-software-services.com/scp/base');
$sale->addChild('description', $orderDescription);
$sale->addChild('amountInMinorUnits', intval($order->get_total() * 100));
它应该输出这个 XML:
<sale xmlns="http://www.capita-software-services.com/scp/simple">
<saleSummary xmlns="http://www.capita-software-services.com/scp/base"/>
<description>Order from Payment Portal</description>
<amountInMinorUnits>2000</amountInMinorUnits>
</sale>
但是正在输出这个...
<sale>
<saleSummary xmlns="http://www.capita-software-services.com/scp/base"/>
<description>Order from Payment Portal</description>
<amountInMinorUnits>2000</amountInMinorUnits>
</sale>
\<sale\>
元素中缺少命名空间。
我实际上在发布这个问题后不久就找到了一种方法。这是解决方案供参考
$sale = $xml->addChild('sale');
$sale->addAttribute('xmlns', 'http://www.capita-software-services.com/scp/simple');
// Add <saleSummary> element with its namespace
$saleSummary = $sale->addChild('saleSummary', null, 'http://www.capita-software-services.com/scp/base');
$saleSummary->addChild('description', $orderDescription);
$saleSummary->addChild('amountInMinorUnits', intval($order->get_total() * 100));