0

我正在添加带有名称空间的属性。看起来我应该以某种方式注册命名空间,但我仍然不知道答案:'( 我的 xml shold 看起来是这样的:

    <?xml version="1.0"?>
<WebOrders xmlns="http://microsoft.com/wsdl/types/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <WebOrder WebOrderId="weborder_id" EComChannelId="1" OrderNo="1" OrderDate="2012-08-03 15:17:42">
        <DefaultLocation  xsi:nil="true"/>
        <GlobalDiscountReason GlobalDiscountReasonId="1" GlobalDiscountAmount="0.00"/>
        <DefaultShippingMethod ShippingMethodId="shipping_id"/>
    </WebOrder>
</WebOrders>

但我不能添加xsi:nil="true"部分。

<?php
class XmlGeneration
{
    public function _construct()
    {
        header('Content-Type: text/xml');
    }

    public function generateXml($params)
    {
        $webOrders = '<WebOrders xmlns="http://microsoft.com/wsdl/types/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"></WebOrders>';
        $webOrders = new SimpleXMLElement($webOrders);
        $this->generateWebOrder($webOrders);
        echo $webOrders->asXML();
    }

    public function generateWebOrder(&$webOrders)
    {
        $result = array(
            'weborder_id' => 1,
            'order_no' => 1,
            'order_date' => 1,
            'discount_id' => 1,
            'discount_amount' => 1,
            'shipping_id' => 1,
            'location_id' => null
        );
        foreach($results as $result)
        {
            $webOrder = $this->createNode($webOrders, 'WebOrder', array('WebOrderId' => $result['weborder_id'], 'EComChannelId' => $this->_channel_id, 'OrderNo' => $result['order_no'], 'OrderDate' => $result['order_date']));

            $this->createNode($webOrder, 'DefaultLocation', array('DefaultLocationId' => $result['location_id']));
            $this->createNode($webOrder, 'GlobalDiscountReason', array('GlobalDiscountReasonId' => $result['discount_id'], 'GlobalDiscountAmount' => $result['discount_amount']));
            $this->createNode($webOrder, 'DefaultShippingMethod', array('ShippingMethodId' => $result['shipping_id']));
        }
    }

    private function createNode(&$parent, $child, $attribute=array())
    {
        $$child = $parent->addChild($child);
        foreach($attribute as $k => $v)
        {
            if($v)
            {
                $$child->addAttribute($k, $v);
            }
            else
            {
                $$child->addAttribute('nil', 'true', 'xsi');
                break;
            }
        }
        return $$child;
    }
}

这是我的代码,我希望使用 registerXPathNamespace 进行命名空间注册,但我之前的每一次试用都返回给我只要。

4

0 回答 0