0

I am using this code to render my rss :

$xml = new SimpleXMLExtended("<?xml version=\"1.0\" encoding=\"utf-8\"?>
            <rss xmlns:g='http://base.google.com/ns/1.0' version='2.0'></rss>");
        $channel = $xml->addChild("channel");
        $channel->addChild("title", "Kidso");
        $channel->addChild("link", "https://kidso.ro");
        $channel->addChild("description", "Kidso");
        foreach ($res as $record) {
            $gtin = $this->db->select('ean13')->where('id_product', $record['pid'])->get('bebs_product')->row();
            $item = $xml->addChild("item");
            $item->addChild("g:id", $record['pid']);
            //$item->addChild("id", $record['pid']);
            $title_feild = "g:title";
            $item->$title_feild = NULL;
            $item->$title_feild->addCData($record['name']);
            $desc_feild = "g:description";
            $item->$desc_feild = NULL;
            $item->$desc_feild->addCData($record['description']);
            $item->addChild("g:link", $record['url']);
            $item->addChild("g:image_link", $record['main_image']);
            $item->addChild("g:availability", $record['qty'] > 0 ? "in stock" : "out of stock");
            $item->addChild("g:sale_price", $record['product_price']);
            $item->addChild("g:gtin", ($gtin ? $gtin->ean13 : ""));
            $brand_feild = "g:brand";
            $item->$brand_feild = NULL;
            $item->$brand_feild->addCData($record['brand_name']);
        }

The class i am using for the CDATA sections:

<?php

class SimpleXMLExtended extends SimpleXMLElement {
    public function addCData($cdata_text) {
        $node = dom_import_simplexml($this);
        $no   = $node->ownerDocument;
        $node->appendChild($no->createCDATASection($cdata_text));
    }
}

Now the CDATA items are shown correct with g: in the begining but on the others, g: is escaped from the names. The result is:

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>Kidso</title>
<link>https://kidso.ro</link>
<description>Kidso</description>
</channel>
<item>
<id>9645</id>
<g:title>
<![CDATA[ 2 in 1 Vaiana wooden puzzle in 25 pieces ]]>
</g:title>
<g:description>
<![CDATA[ Type: 2D Age: 3+ years Number of items: 50 Product size: 70 x 50 cm. Package size: 25 x 6 x 17 cm Weight: 0.6 kg Material: Wood Additional Features: The set includes 2 puzzles of 25 pieces of environmentally sustainable and safe wood that has been treated to avoid splinters. The product allows the child to get pictures of the characters from the Vaiana Disney movie through arrangement and assembly. It is made of environmentally sustainable and safe plastic coated wood. By arranging the puzzle, the child will develop his analytical thought, logic and dexterity. Attention, the product contains small parts that the child can swallow! EDUCA is a leading brand in educational games and puzzles. It offers selected collections of high quality materials and perfect match puzzles. ]]>
</g:description>
<link>https://kidso.bg/darven-pazel-2-v-1-vaiana-9645.html</link>
<image_link>https://backoffice.kidso.com/selmatic/image/9645?code=18327923</image_link>
<availability>in stock</availability>
<sale_price>41.95</sale_price>
<gtin>8412668169494</gtin>
<g:brand>
<![CDATA[ Educa ]]>
</g:brand>
</item>
</rss>

How to prevent this escaping? I need all fields to be with g: prefix.

Toma Tomov
  • 1,476
  • 19
  • 55
  • Does this answer your question? [adding a namespace when using SimpleXMLElement](https://stackoverflow.com/questions/6927567/adding-a-namespace-when-using-simplexmlelement) – CBroe Mar 24 '21 at 09:24
  • It happens this `9645` when I use it this way `$item->addChild("g:id", $record['pid'], 'g');`. The name is fine but I dont need the `xmlns:g="g"` part. This is problem. – Toma Tomov Mar 24 '21 at 09:28
  • 1
    It should be `$item->addChild('g:id', $record['pid'], 'http://base.google.com/ns/1.0')`. This should not add an additional `xmlns:g`. The namespace is defined for the prefix `g` on the document element. – ThW Mar 24 '21 at 10:20
  • @ThW. Thank you. That's correct :) – Toma Tomov Mar 24 '21 at 12:55

0 Answers0