【问题标题】:remove attribute in xml删除xml中的属性
【发布时间】:2018-12-11 15:45:49
【问题描述】:

我正在用 PHP 构建一个 RSS 提要,但我对命名空间有点困惑 这是我的代码

<?php

header("Content-Type: application/rss+xml; charset=UTF-8");
$xml = new SimpleXMLElement('<rss></rss>', 0 , false , 'media' , true);
$xml->addAttribute("version", "2.0");
$xml->addAttribute("xmlns:xmlns:media" ,"http://search.yahoo.com/mrss/");

$channel = $xml->addChild("channel");
$channel->addChild("title", "xxxxxx.fr");
$channel->addChild("link", "http://www.xxxxxx.fr");
$channel->addChild("description", "XXXXXXX");
$channel->addChild("language", "fr-fr");

foreach($data as $key => $entry){

    $item = $channel->addChild("item");
    $guid = $item->addChild("guid", $entry['video_id']);
    $guid->addAttribute('isPermalink' , 'false');
    $item->addChild("title", "<![CDATA[ " .htmlspecialchars($entry['title']). " ]]>");

    $title = $item->addChild('media:title', "<![CDATA[ " .htmlspecialchars($entry['title']). " ]]>" , 'media');
    $title->addAttribute('type' ,'plain');

    $pubdate = $item->addChild("pubDate" , $entry['created_time']->format('Y-m-d H:i:s'));

    $media = $item->addChild('media:content', null , 'media');
    $media->addAttribute('duration', $entry['duration']);
    $media->addAttribute('url', $entry['source_video']);

    $thumbnail = $media->addChild('media:thumbnail' , null , 'media');
    $thumbnail->addAttribute('height' , '1080');
    $thumbnail->addAttribute('width' , '1080');
    $thumbnail->addAttribute('url', $entry['picture_path']);

    $category = $media->addChild('media:category', $iab , 'media');
    $category->addAttribute('scheme' , 'https://support.aerserv.com/hc/en-us/articles/207148516-List-of-IAB-Categories');

}

echo preg_replace('/xmlns:xmlns="media"\s?/', '', $xml->asXML($page_name.'-2.xml'));

?>

还有输出

<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        <title>xxxx.fr</title>
        <link>http://www.xxxx.fr</link>
        <description>
            XXXXX
        </description>
        <language>fr-fr</language>
        <item>
            <guid isPermalink="false">1947196298709465</guid>
            <title>
                <![CDATA[ Cette artiste utilise la gélatine comme support pour créer des œuvres en 3 dimensions ]]>
            </title>
            <media:title xmlns:media="media" type="plain">
                <![CDATA[ Cette artiste utilise la gélatine comme support pour créer des œuvres en 3 dimensions ]]>
            </media:title>
            <pubDate>2018-12-10 19:30:01</pubDate>
            <media:content xmlns:media="media" duration="216.723" url="https://scontent.xx.fbcdn.net/v/t66.18014-6/10000000_2xxxxx_861713768647979845_n.mp4?_nc_cat=100&efg=eyJ2ZW5jb2RlX3RhZyI6Im9lcF9oZCJ9&_nc_ht=scontent.xx&oh=9cfb72ebc8fc2bdfd8ae7816899b29e2&oe=5CAC9FBC">
                <media:thumbnail height="1080" width="1080" url="https://scontent.xx.fbcdn.net/v/t15.5256-10/47695217_1947199588709136_5040968xxxxx1952_n.jpg?_nc_cat=1&_nc_ht=scontent.xx&oh=ea3af9f82426e5a3f9ca1a3e907b09ca&oe=5CAE1DBB"/>
                <media:category scheme="https://support.aerserv.com/hc/en-us/articles/207148516-List-of-IAB-Categories">IAB8</media:category>
            </media:content>
        </item>
    </channel>
</rss>

我想要的是删除 rss 节点中预期的所有 xmlns:media="media" 也许我在命名空间上做错了我不知道为什么它在 media:title 或 media:content 中添加 xmlns:media="media"

【问题讨论】:

    标签: php xml rss simplexml


    【解决方案1】:

    在涉及命名空间时,SimpleXML 不太擅长生成干净的 XML,尽管您添加的元素略有错误...

    $media = $item->addChild('media:content', null , 'media');
    

    第三个参数需要是 URI 而不是前缀...

    $media = $item->addChild('media:content', null , "http://search.yahoo.com/mrss/");
    

    第二部分(虽然需要先完成)是如何添加命名空间。一旦在 XML 层次结构中“正确”定义了命名空间,SimpleXML 就可以不重新声明它,但由于目前使用 addAttribute() 添加它的方式实际上并没有声明它,更多的是添加一个带有设置的属性...

    $xml->addAttribute("xmlns:xmlns:media" ,"http://search.yahoo.com/mrss/");
    

    一种伪造的方法是向根元素添加一个需要新命名空间的属性,然后将其删除。幸运的是,SimpleXML 没有清除声明,并且现在为整个文档设置了它...

    $xml->addAttribute("media:a", "temp", "http://search.yahoo.com/mrss/");
    unset($xml->attributes("media", true)["a"]);
    

    你也可以去掉最后一个替换...

    echo $xml->asXML($page_name.'-2.xml'));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多