PHP's SimpleXML: How to use colons in names

﹥>﹥吖頭↗ 提交于 2019-11-27 22:56:35

As @ceejayoz said, you need to add the "http://base.google.com/ns/1.0" namespace to the root node so that SimpleXML knows the namespace has already been declared and doesn't emit a duplicate prefix binding.

I think you may need to read a tutorial on XML Namespaces, because I'm not sure you really understand what the "g:" is doing here.

Here is a more complete example. XML:

$xml = <<<EOT 
<?xml version="1.0"?> 
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"> 
  <channel> 
    <title>The name of your data feed</title> 
    <link>http://www.example.com</link> 
    <description>A description of your content</description> 
    <item> 
      <title>Red wool sweater</title> 
      <link> http://www.example.com/item1-info-page.html</link> 
      <description>Comfortable and soft, this sweater will keep you warm on those cold winter nights.</description> 
      <g:image_link>http://www.example.com/image1.jpg</g:image_link> 
      <g:price>25</g:price> 
      <g:id>1a</g:id> 
    </item> 
  </channel> 
</rss> 
EOT 
; 

Code:

$rss = new SimpleXMLElement($xml); 
$NS = array( 
    'g' => 'http://base.google.com/ns/1.0' 
); 
$rss->registerXPathNamespace('g', $NS['g']); 
$product = $rss->channel->item[0]; // example 

// Use the complete namespace. 
// Don't add "g" prefix to element name--what prefix will be used is 
// something SimpleXML takes care of. 
$product->addChild('condition', 'new', $NS['g']); 

echo $rss->asXML(); 

I usually use this pattern to deal with namespaces easily:

$rss = new SimpleXMLElement($xml); 
$NS = array( 
    'g' => 'http://base.google.com/ns/1.0' 
    // whatever other namespaces you want 
); 
// now register them all in the root 
foreach ($NS as $prefix => $name) { 
    $rss->registerXPathNamespace($prefix, $name); 
} 
// Then turn $NS to an object for more convenient syntax 
$NS = (object) $NS; 
// If I need the namespace name later, I access like so: 
$element->addChild('localName', 'Value', $NS->g); 
Alex Toro

This do the trick:

$product->addChild('xmlns:g:condition', 'new'); 

You need to add that namespace to a parent node, preferably the root rss one, so that the child nodes can inherit it instead of having to explicitly specify it each time.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!