首页 > 解决方案 > RSS 提要在某些项目中不显示 g: 部分

问题描述

我正在使用此代码来呈现我的 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']);
        }

我用于 CDATA 部分的类:

<?php

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

现在 CDATA 项目在开头显示正确,g:但在其他项目中,g:从名称中转义。结果是:

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

如何防止这种逃逸?我需要所有字段都带有g:前缀。

标签: phpxmlrss

解决方案


推荐阅读