2014-05-13 165 views
1

我想將名稱空間屬性'xmlns'保存在xml文檔中。 以下是我已經嘗試過做..將xmlns屬性添加到XML文件。

$xml = new DOMDocument(); 
$xml_index = $xml->createElement("urlset"); 
$root->appendChild(
$xml->createAttribute('xmlns'))->appendChild(
$xml->createTextNode('http://www.sitemaps.org/schemas/sitemap/0.9')); 
for ($i = 0; $i < $count; $i++) { 
    $xml_sitemap = $xml->createElement("url"); 
    $xml_loc = $xml->createElement("loc", 'http://test.com/sitemaps/'.$link[$i]); 
    $xml_index->appendChild($xml_sitemap); 
    $xml_sitemap->appendChild($xml_loc); 
} 
$xml_index->appendChild($xml_sitemap); 
$xml_sitemap->appendChild($xml_loc); 
$xml->appendChild($xml_index); 
$xml->save('\tmp\test.xml'); 

有人可以幫我嗎? 我想要的格式如下

<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 
    <sitemap> 
      <loc>http://test.com/sitemaps/urlset_start10000.xml</loc> 
    </sitemap> 
    <sitemap> 
      <loc>http://test.com/sitemaps/urlset_start20000.xml</loc> 
    </sitemap> 
</sitemapindex> 

輸出我得到的是,沒有xmlns屬性。

回答

0

documentation狀態createElementNS應該做的工作

$dom = new DOMDocument('1.0', 'utf-8'); 
$element = $dom->createElementNS('http://www.example.com/XFoo', 'xfoo:test', 'This is the root element!'); 
$dom->appendChild($element); 
+0

感謝。但這不是她想要的。她想在節點中添加xmlns屬性而不是值。 – atpatil11