PHP's SimpleXML: How to use colons in names

Posted by nute on Stack Overflow See other posts from Stack Overflow or by nute
Published on 2010-06-10T21:48:55Z Indexed on 2010/06/10 21:52 UTC
Read the original article Hit count: 572

Filed under:
|
|
|

I am trying to generate an RSS Google Merchant, using SimpleXML.

The sample given by Google is:

<?xml version="1.0"?>
<rss version="2.0" 
xmlns:g="http://base.google.com/ns/1.0">
<channel>
<title>The name of your data feed</title>
<link>http://www.example.com</link>
<description>A description of your content</description>
<item>
<title>Red wool sweater</title>
<link> http://www.example.com/item1-info-page.html</link>
<description>Comfortable and soft, this sweater will keep you warm on those cold winter nights.</description>
<g:image_link>http://www.example.com/image1.jpg</g:image_link> <g:price>25</g:price> <g:condition>new</g:condition> <g:id>1a</g:id>
</item>
</channel>
</rss>

My code has things like:

$product->addChild("g:condition", 'new');

Which generates:

<condition>new</condition>

I read online that I should instead use:

$product->addChild("g:condition", 'new', 'http://base.google.com/ns/1.0');

Which now generates:

<g:condition xmlns:g="http://base.google.com/ns/1.0">new</g:condition>

This seems very counter-intuitive to me, as now the "xmlns" declaration is on almost EVERY line of my RSS feed intead of just once in the root element.

Am I missing something?

© Stack Overflow or respective owner

Related posts about php

Related posts about Xml