SimpleXML: adding “xmlns:wp” attribute adds only “wp”, why?

Im trying to create WXR file from scratch (WordPress eXtended Rss).

My code is based on XML/WXR file generated by wordpress and starts like this:

Read More
<?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/"
>

I started like this:

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

After this im printing XML like this:

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

Im getting XML like this:

<?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>

(i added line breaks for better readability).

How i can force full attributes, like “xmlns:excerpt” instead of just “excerpt”?

Related posts

Leave a Reply

1 comment

  1. The part before the colon is called “namespace” and needs to be specified separately. For addAttribute it is the third parameter:

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