views:

118

answers:

1

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"&gt;
<channel>
<title>The name of your data feed</title>
<link>http://www.example.com&lt;/link&gt;
<description>A description of your content</description>
<item>
<title>Red wool sweater</title>
<link> http://www.example.com/item1-info-page.html&lt;/link&gt;
<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&lt;/g:image_link&gt; <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"&gt;new&lt;/g:condition&gt;

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?

+1  A: 

You need to add that namespace to a parent node, preferably the root rss one, so that the child nodes can inherit it instead of having to explicitly specify it each time.

ceejayoz
I tried`$xml = new SimpleXMLElement('<rss></rss>');$xml->addAttribute('version','2.0');$xml->registerXPathNamespace('g', 'http://base.google.com/ns/1.0');`And then later:`$product->addChild("g:condition", 'new');`It generates:`<condition>new</condition>`It skips "g:" again ...
nute
You need to still specify the namespace in the `addChild` calls. It just shouldn't be output because the root node already has it.
ceejayoz