PHP SimpleXML 选择属性

发布于 2024-09-19 22:53:11 字数 1343 浏览 3 评论 0原文

我有一个如下所示的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍风燃霜 2024-09-26 22:53:11

你有一个错字!

它:

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"];
    }
}


祝你好运

You have a typo!

Its:

echo $item->categories->category->attributes()->name;

Not:

echo $item->category->attributes()->name;

For everything:

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"];
    }
}


Good Luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文