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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色