PHP XPATH within changing XML structure
I've an XML file like this:
<tr class="station">
<td class="realtime">
<span>
15:11
</span>
</td>
</tr>
<tr class="station">
<td class="clock">
15:20
</td>
</tr>
<tr class="station">
<td class="clock">
15:30
</td>
</tr>
<tr class="station">
<td class="realtime">
<span>开发者_C百科
15:41
</span>
</td>
</tr>
and I wanna parse it with xpath in php. The xml is been updated and parsed quite often.
I always want to get the first time (in this case 15:11) The problem is that its not sure whether the surrounding tag is a td by class "clock" or "realtime". If there is a so surrounding realtime, then there is a span tag within. Otherwise not. In fact, its always the first "station"-class tag in which the information is, that matters. So is it possible to tell xpath to just evaluate within this tag? Is there a good method for doing this in xpath? (sry for my bad english)In fact, its always the first "station"-class tag in which the information is, that matters. So is it possible to tell xpath to just evaluate within this tag?
With this wellformed input source:
<table>
<tr class="station">
<td class="realtime">
<span>
15:41
</span>
</td>
</tr>
<tr class="station">
<td class="clock">
15:20
</td>
</tr>
<tr class="station">
<td class="clock">
15:30
</td>
</tr>
<tr class="station">
<td class="realtime">
<span>
15:41
</span>
</td>
</tr>
</table>
This XPath expression:
/table/tr[@class='station'][1]/td
Note: Just select the element you want and use the proper DOM API method to get the string value. It doesn't matter whether there is a span
element or not.
If you want to...
/table/tr[@class='station'][1]/td//text()
精彩评论