PHP addChild function misssing the namespace from the xml

  Kiến thức lập trình

Hoping someone can shed some light on why this is happening.

I’ve used the exact same syntax elsewhere in my code without any issues, but for some reason this isn’t working:

$sale = $xml->addChild('sale', null, 'http://www.capita-software-services.com/scp/simple');

In my code I have the following:

$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));

which is supposed to output this 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>

but is outputting this…

<sale>
   <saleSummary xmlns="http://www.capita-software-services.com/scp/base"/>
   <description>Order from Payment Portal</description>
   <amountInMinorUnits>2000</amountInMinorUnits>
</sale>

The namespace is missing from the <sale> element.

Recognized by PHP Collective

2

I actually found a way to do it not long after posting this question. For reference this is the solution

        $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));

4

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT