我有一个XML Feed,如下所示:
<?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>
我正在尝试使用此脚本打印出category属性:
<?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插件告诉我我也正确选择属性。
答案 0 :(得分:4)
你有一个错字!
其:
echo $item->categories->category->attributes()->name;
不会强>
echo $item->category->attributes()->name;
对于一切:
foreach($result as $item) { echo $item->name; echo $item->imageURL; echo $item->description; echo $item->price; echo $item->category->attributes()->name; foreach($item->additional->field as $field) { echo $field["name"]; echo $field["value"]; } }
祝你好运