使用xPath的PHP查询XML
我的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>
<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结果的代码中。看
另见:
- 及
/Tickets/EventsPoints/Event[PerformanceName[.="U2"]]
$event->parentNode