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 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真