通过parent属性值的访问 [英] Access value through parent attribute

查看:343
本文介绍了通过parent属性值的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找基于一个通过attribue通过XPath来获取值A-1。

I am looking to get the value A-1 through xpath based on a passed attribue.

我已经通过从previous页PHP通过该单元的索引属性和我受全球GET访问它:

I have passed the index attribute of the unit through php from a previous page and am accessing it by global GET:

$value = intval($_GET['index']);

在xml:

<UNIT index='1'>
     <ID>A-1</ID>
     <MANUFACTURER>testing inc.</MANUFACTURER>
</UNIT>
<UNIT index='2'>
     <ID>A-2</ID>
     <MANUFACTURER>testing inc.</MANUFACTURER>
</UNIT>

我试图用呼应一下:

I'm trying to echo it out using:

$xml = new SimpleXMLElement('demo.xml',NULL,true);

echo $xml->UNIT[$value]->ID;

我知道我得到,因为我回显的$值来检查,我需要通过通过1,而
它给我A-2的ID,这将是XML索引编号(从0开始) - 不是我的属性索引号

I know i'm getting the "1" that I need passed through because I echo'd $value to check, but its giving me the ID of A-2, which would be the xml index number (starting from 0) - not my attribute index number.

推荐答案

您可以使用 :: SimpleXMLElement对象的XPath 方法来查询的具体单元您要使用XPath查询像 / / UNIT [@指数= 2]

You can use the SimpleXMLElement::xpath method to query for the specific UNIT that you want with an XPath query like //UNIT[@index=2].

$value = intval($_GET['index']);
$xml   = new SimpleXMLElement('demo.xml',NULL,true);
$units = $xml->xpath("//UNIT[@index=$value]"); // xpath returns an array
if (isset($units[0])) {
    echo $units[0]->ID;
} else {
    echo "No unit with index $value";
}

这篇关于通过parent属性值的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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