Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xPath的PHP查询XML_Php_Xml_Xpath - Fatal编程技术网

使用xPath的PHP查询XML

使用xPath的PHP查询XML,php,xml,xpath,Php,Xml,Xpath,我的XML结构如下所示: <Tickets> <EventsPoints> <Event ID="23"> <PerformanceName>U2</PerformanceName> <EventDate>25/05/2012</EventDate> <EventPrice>75.00</EventPrice>

我的XML结构如下所示:

<Tickets>
<EventsPoints>
      <Event ID="23">
           <PerformanceName>U2</PerformanceName>
           <EventDate>25/05/2012</EventDate>
           <EventPrice>75.00</EventPrice>
      </Event>
      <Event ID="27">
           <PerformanceName>Jedward</PerformanceName>
           <EventDate>28/05/2012</EventDate>
           <EventPrice>20.00</EventPrice>
      </Event>
            <Event ID="27">
           <PerformanceName>Rolling Stones</PerformanceName>
           <EventDate>03/12/2012</EventDate>
           <EventPrice>80.00</EventPrice>
      </Event>
</EventsPoints>
</Tickets>

当需要父元素时,XPath将获取
PerformanceName
元素。换成

/Tickets/EventsPoints/Event/PerformanceName[.="U2"]/..

或进口

$event->parentNode
此外,您不需要为每个人设置第一个
foreach
。删除它并将写入
$dstDom
的代码移动到迭代XPath结果的代码中。看

另见:


我们的想法是搜索性能名称,然后返回有效的事件详细信息hanks,唯一的问题是,如果有两个具有相同性能名称的事件,它只返回first@user我很肯定你会想出如何用给定的信息来改变这一点。也必须有一些东西让你去发现;)
/Tickets/EventsPoints/Event[PerformanceName[.="U2"]]
$event->parentNode