SimpleXML文档上的xpath查询-未定义偏移量:0 [英] xpath query on SimpleXML Document - Undefined offset: 0

查看:86
本文介绍了SimpleXML文档上的xpath查询-未定义偏移量:0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对SimpleXML相当陌生,并在其中使用了xpath。我不确定是否要在原始发布中还是在此处发布此查询,所以我链接到原始发布到此处。

I'm quite new to SimpleXML and using xpath within it. I wasn't sure whether to post this query within the original posting or here, so I linked to here form the original posting.

在处理多个测试脚本时,我遇到了发布,而我一直在尝试制作一个被接受的答案的工作。
但是,当我尝试使用接受的答案作品的xpath版本时,出现错误消息:

In working through several test scripts, I came across this posting, and I've been attempting to make one of the accepted answers work. However, when I attempt to use the xpath version of the accepted answer work, I get the error message:

Undefined offset: 0 

我已经以其他方式测试了xpath,以验证它是否已安装且功能正常。谢谢您的指导。

I've tested xpath in other ways, to verify that it is installed and functional. Thanks for any leads.

我复制了OP的xml文件以及可接受的答案代码:

I have duplicated the OP's xml file, along with the accepted answer code:

XML:

<data>
  <poster name="E-Verify" id="everify">
    <full_image url="e-verify-swa-poster.jpg"/>
    <full_other url=""/>
  </poster>
  <poster name="Minimum Wage" id="minwage">
    <full_image url="minwage.jpg"/>
    <full_other url="spa_minwage.jpg"/>
  </poster>
</data>

PHP代码:

<?php
$url = "test_b.xml";
$xml = simplexml_load_file($url);
$main_url = $xml->xpath('name[@id="minwage"]/full_image')[0];
?>


推荐答案

您的XPath正在寻找一个不存在的元素,称为名称并没有找到它。这意味着使用 [0] 无需提取0元素。我已将元素名称更改为 poster ,在本示例中,我获取了 url 属性,但是您可以更改它到您需要的其他任何内容...

Your XPath was looking for a non-existant element called name and not finding it. This will mean there isn't a 0 element to fetch using the [0]. I've changed the element name to poster and in this example I fetch the url attribute, but you can change it to whatever else you need...

$main_url = $xml->xpath('//poster[@id="minwage"]/full_image/@url')[0];
echo $main_url;

给予...

minwage.jpg

这篇关于SimpleXML文档上的xpath查询-未定义偏移量:0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆