0

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.

1

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I wrote FROM performances, but I was trying to emphasize that I want it to come from a specific performance ID element. I am using only PHP.
That's exactly what the XPath does. It selects only reservations from a performance with a particular ID. (PS: What does "I am using only PHP" mean?)
Sorry, I'm just really confused. I need to write some PHP which will grab the 3 reservation seat values from the element performance which has the ID of 7.
@Henryz: Did you follow the link in my answer and read the documentation there? (Your confusion indicates you didn't.) Also read a few basic things about XPath, it's really easy to pick up.
Hi, I did look but didn't quite get it, I followed on to a W3Schools reference and had a read. I used your xpath piece of code and it works great. Thanks very much, appreciate it.
|
1
<?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.

1 Comment

Hey Cory, I'm new to both XML and XPATH so I did find it hard to grasp the concept. I don't suppose you would know if it is possible to write (addAtrribute) using xpath as well?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.