2013-02-11 135 views
2

我試圖從頭開始創建WXR文件(WordPress eXtended Rss)。SimpleXML:添加「xmlns:wp」屬性只會添加「wp」,爲什麼?

我的代碼是基於WordPress的生成的XML/WXR文件,並開始像這樣:

<?xml version="1.0" encoding="UTF-8" ?> 

<rss version="2.0" 
    xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/" 
    xmlns:content="http://purl.org/rss/1.0/modules/content/" 
    xmlns:wfw="http://wellformedweb.org/CommentAPI/" 
    xmlns:dc="http://purl.org/dc/elements/1.1/" 
    xmlns:wp="http://wordpress.org/export/1.2/" 
> 

我開始像這樣:

$newxml = new SimpleXMLElement("<rss></rss>"); 
$newxml->addAttribute('version', '2.0'); 
$newxml->addAttribute('xmlns:excerpt', 'http://wordpress.org/export/1.2/excerpt/'); 
$newxml->addAttribute('xmlns:content', 'http://purl.org/rss/1.0/modules/content/'); 
$newxml->addAttribute('xmlns:wfw', 'http://wellformedweb.org/CommentAPI/'); 
$newxml->addAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); 
$newxml->addAttribute('xmlns:wp', 'http://wordpress.org/export/1.2/'); 

該即時通訊的印刷XML後像這樣:

echo htmlspecialchars($newxml->asXML()); 

即時得到這樣的XML:

<?xml version="1.0"?> 
<rss version="2.0" 
    excerpt="http://wordpress.org/export/1.2/excerpt/" 
    content="http://purl.org/rss/1.0/modules/content/" 
    wfw="http://wellformedweb.org/CommentAPI/" 
    dc="http://purl.org/dc/elements/1.1/" 
    wp="http://wordpress.org/export/1.2/"> 

    <channel/> 
</rss> 

(我加換行以提高可讀性)。

我如何強制全部屬性,如「xmlns:摘錄」而不是「摘錄」?

回答

3

冒號前的部分稱爲「名稱空間」,需要單獨指定。對於addAttribute它是第三個參數:

$newxml->addAttribute ('excerpt', 'http://wordpress.org/export/1.2/excerpt/', 'xmlns'); 
+0

謝謝。它有幫助,但我有另一個問題,當我需要添加'',但它看起來像一個主題爲下一個問題:) – Kamil 2013-02-12 23:09:56

+0

驚喜:'addChild'也有一個「命名空間」參數;) – 2013-02-13 06:14:05

+0

不真。我試過這個。我想要類似於這個'',即時獲得。它將其添加爲「xmlns」屬性。 – Kamil 2013-02-14 18:05:01

相關問題