duandaotui5633 2014-04-03 16:51
浏览 229
已采纳

使用PHP在XML子元素名称中包含冒号

I'm trying to have my sitemap update automatically as pages are added. I'm defining var containing the child name I need, which is including a colon character. PHP or XML is removing the colon and word to the right or left of it. How can I keep that colon in the child elements name?

I'm using this:

<?php
$imagechild = 'image:image';
$imageloc = 'image:loc';

 $xml=simplexml_load_file("sitemap.xml");

 $map = $xml->addChild('url');
   $map->addChild('loc', "http:/some website".$page_path);

 $img = $map->addChild($imagechild);
   $img->addChild($imageloc, $img_link);

     $xml->saveXML('sitemap.xml');
?>

I'm getting this:

      <url>
        <loc>web url</loc>
        <image>
          <loc>image url</loc>
        </image>
      </url>

I need this

      <url>
        <loc>web url</loc>
        <image:image>
          <loc>image url</loc>
        </image:image>
      </url>

Thank you in advance!

  • 写回答

1条回答 默认 最新

  • duanbi3786 2014-04-03 17:02
    关注

    If an element name contains a : then the part before the : is the namespace prefix. If you are using namespace prefixes then you need to define the namespace somewhere in the document.

    Check the manual of SimpleXmlElement::addChild(). You need to pass the namespace uri as the third element in order to make it work:

    $img = $map->addChild($imagechild, '',  'http://your.namspace.uri/path');
    

    I would encourage you to use the DOMDocument class in favour of the simple_xml extension. It can handle namespaces much more properly. Check this example:

    Assuming you have this xml:

    <?xml version="1.0"?>
    <map>
    </map>
    

    And this PHP code:

    $doc = new DOMDocument();
    $doc->load("sitemap.xml");
    
    $map = $doc->documentElement;
    
    // Define the xmlns "image" in the root element
    $attr = $doc->createAttribute('xmlns:image');
    $attr->nodeValue = 'http://your.namespace.uri/path';
    $map->setAttributeNode($attr);
    
    // Create new elements
    $loc = $doc->createElement('loc', 'your location comes here');
    $image = $doc->createElement('image:image');
    $imageloc = $doc->createElement('loc', 'your image location comes here');
    
    // Add them to the tree
    $map->appendChild($loc);
    $image->appendChild($imageloc);
    $map->appendChild($image);
    
    // Save to file
    file_put_contents('sitemap.xml', $doc->saveXML());
    

    You'll get this output:

    <?xml version="1.0"?>
    <map xmlns:image="http://your.namespace.uri/path">
      <loc>your location comes here</loc>
      <image:image>
        <loc>your image location comes here</loc>
      </image:image>
    </map>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图