PHP SimpleXML 选择属性
我有一个如下所示的 XML 提要:
<?xml version="1.0" encoding="UTF-8"?>
<productFeed version="1.0" timestamp="20100910:04:06:55">
<product id="196">
<name>Lorem</name>
<price>190.00</price>
<description>Lorem Ipsum</description>
<productURL> http://www.example.com</productURL>
<imageURL> http://www.example.com/lorem.jpg </imageURL>
<additional>
<field name="country" value="IT" />
<field name="region" value="Rome" />
<field name="city" value="Rome" />
</additional>
<categories>
<category name="foobar" />
</categories>
</product>
</productFeed>
我正在尝试使用此脚本打印出类别属性以及其他内容:
<?php
$url = 'lorem.xml';
$xml = simplexml_load_file($url);
$expr = "/productFeed//product/additional/field[@value='Rome']/parent::*/parent::*";
$result = $xml->xpath($expr);
foreach($result as $item) {
echo $item->name;
echo $item->imageURL;
echo $item->description;
echo $item->price;
echo $item->category->attributes()->name;
}
这会生成:
警告:main() [function.main]:节点不再存在
错误,尽管 FireXPath Firebug 插件告诉我我也正确选择了属性。
I have an XML feed that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<productFeed version="1.0" timestamp="20100910:04:06:55">
<product id="196">
<name>Lorem</name>
<price>190.00</price>
<description>Lorem Ipsum</description>
<productURL> http://www.example.com</productURL>
<imageURL> http://www.example.com/lorem.jpg </imageURL>
<additional>
<field name="country" value="IT" />
<field name="region" value="Rome" />
<field name="city" value="Rome" />
</additional>
<categories>
<category name="foobar" />
</categories>
</product>
</productFeed>
I'm trying to print out the category attribute, among other things with this script:
<?php
$url = 'lorem.xml';
$xml = simplexml_load_file($url);
$expr = "/productFeed//product/additional/field[@value='Rome']/parent::*/parent::*";
$result = $xml->xpath($expr);
foreach($result as $item) {
echo $item->name;
echo $item->imageURL;
echo $item->description;
echo $item->price;
echo $item->category->attributes()->name;
}
This produces a:
Warning: main() [function.main]: Node no longer exists
error although the FireXPath Firebug plugin tells me I'm correctly selecting the attributes too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有一个错字!
它:
不是:
对于一切:
祝你好运
You have a typo!
Its:
Not:
For everything:
Good Luck