Reputation: 1599
A user can select a performance, so the performance elements have unique ID's. I want to basically say:
Select all reservations->reservation['seat'] FROM performances->performance['id=2'].
Hope that makes sense, I really am struggling with selections in simple XML.
Thanks in advance, Henry.
<performances>
<performance id="7" time="12:00" day="Monday" month="June" year="2010">
<reservations>
<reservation seat="a7"/>
<reservation seat="a2"/>
<reservation seat="a3"/>
</reservations>
</performance>
<performance id="8" time="12:00" day="Tuesday" month="June" year="2010">
<reservations>
<reservation seat="a8"/>
</reservations>
</performance>
</performances>
Extra Info:
I am currently using: echo $xml2->show->performances->performance['0']->reservations->reservation['seat']
This gets one reservation from the first performance, but I want all reservations from the performance with the id of '3' for example.
Upvotes: 0
Views: 1662
Reputation: 893
<?php
// create a variable repsresenting the xml to parse through
$string = '<performances>
<performance id="7" time="12:00" day="Monday" month="June" year="2010">
<reservations>
<reservation seat="a7"/>
<reservation seat="a2"/>
<reservation seat="a3"/>
</reservations>
</performance>
<performance id="8" time="12:00" day="Tuesday" month="June" year="2010">
<reservations>
<reservation seat="a8"/>
</reservations>
</performance>
</performances>';
// create a variable representing a SimpleXMLElement instance
$xml = simplexml_load_string($string);
// create a variable representing the desired results, using the xpath method for
// the SimpleXMLElement instance previously created.
$results = $xml->xpath('//performances/performance[@id=2]/reservations/reservation/@seat');
?>
Sorry to steal Tomalak's thunder here. Some folks need it spelled out sometimes.
Upvotes: 1
Reputation: 338208
Your "SQL":
SELECT reservations->reservation['seat'] FROM performances->performance['id=2']
can be reformulated as:
FROM performances->performance['id=2'] SELECT reservations->reservation['seat']
can be reformulated as XPath:
//performances/performance[@id=2]/reservations/reservation/@seat
Ant that's what you need. Look at SimpleXML's xpath()
method.
Upvotes: 2