2017-07-20 170 views
0

我想實現下面以XML格式開始元素(PHP生成):PHP - 在XML命名空間 - 與命名空間

<ns0:XmlInterchange xmlns:ns0="http://www.website.com" xmlns:ext="http://www.website.com"> 
    <ns0:InterchangeInfo> 
     <ns0:Date>2017-06-28T11:33:15</ns0:Date> 
     <ns0:XmlType>Verbose</ns0:XmlType> 
     <ns0:Source> 
     <ns0:EnterpriseCode>DSV</ns0:EnterpriseCode> 
     <ns0:OriginServer>ESB</ns0:OriginServer> 
     </ns0:Source> 
     <ns0:EDIOrganisation EDICode="0"/> 
    </ns0:InterchangeInfo> 
</ns0:XmlInterchange> 

所以,現在,我的一切是這樣的:

$xml = new DOMDocument('1.0', 'UTF-8'); 
$xml->formatOutput = true; 

$rss = $xml->createElement('ns0'); 
$rss->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns0', 'http://website.com'); 
$rss->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ext', 'http://website.com'); 
$xml->appendChild($rss); 

其輸出這樣的:

<ns0 xmlns:ns0="http://website.com" xmlns:ext="http://website.com"/> 

以上的問題是XML的開始。它需要:

<ns0:XmlInterchange xmlns:ns0="http://website.com" xmlns:ext="http://website.com"/> 

我怎樣才能:XmlInterchange命名空間添加到標籤的開始?

回答

1

你應該使用:

$rss = $xml->createElementNS('ns0', 'ns0:XmlInterchange'); 

,而不是

$rss = $xml->createElement('ns0');