PHP的SimpleXML:如何在名称中使用冒号 [英] PHP's SimpleXML: How to use colons in names
问题描述
我正在尝试使用SimpleXML生成RSS Google商家.
I am trying to generate an RSS Google Merchant, using SimpleXML.
Google提供的示例是:
The sample given by Google is:
<?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:condition>new</g:condition> <g:id>1a</g:id>
</item>
</channel>
</rss>
我的代码包含以下内容:
My code has things like:
$product->addChild("g:condition", 'new');
哪个生成:
<condition>new</condition>
我在网上阅读了我应该改用的内容:
I read online that I should instead use:
$product->addChild("g:condition", 'new', 'http://base.google.com/ns/1.0');
现在会生成:
<g:condition xmlns:g="http://base.google.com/ns/1.0">new</g:condition>
这对我来说似乎是违反直觉的,因为现在"xmlns"声明几乎在我的RSS feed intead的每行中都只在根元素中出现一次.
This seems very counter-intuitive to me, as now the "xmlns" declaration is on almost EVERY line of my RSS feed intead of just once in the root element.
我想念什么吗?
推荐答案
正如@ceejayoz所说,您需要将"http://base.google.com/ns/1.0"命名空间添加到根节点,以便SimpleXML知道名称空间已经声明,并且不会发出重复的前缀绑定.
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.
我认为您可能需要阅读有关XML命名空间的教程,因为我不确定您是否真的了解"g:"在这里的作用.
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.
这是一个更完整的示例. XML:
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
;
代码:
$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);
这篇关于PHP的SimpleXML:如何在名称中使用冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!