tags:

views:

95

answers:

2

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.

+2  A: 

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.

Tomalak
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.
Henryz
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?)*
Tomalak
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
@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](http://www.w3schools.com/xpath/default.asp), it's really easy to pick up.
Tomalak
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.
Henryz
@Henryz: You can play around with [an online XPath tester](http://www.mizar.dk/XPath/Default.aspx) to get a hang of it interactively.
Tomalak
A: 
<?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.

Cory Collier
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?
Henryz