2010-08-08 6 views
0

사용자는 성능을 선택할 수 있으므로 성능 요소에는 고유 한 ID가 있습니다. 기본적으로 다음과 같이 말하고 싶습니다 :단순 XML - 특정 속성 값을 갖는 모든 요소를 ​​선택하는 방법

모든 예약 -> [좌석] FROM performances-> performance [ 'id = 2']를 선택하십시오.

호프 (Hope)는 이해하기 쉽지만, 나는 단순한 XML의 선택에 정말로 고심하고있다.

미리 감사드립니다. 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> 

추가 정보 :

내가 현재 사용하고 있습니다 : echo $xml2->show->performances->performance['0']->reservations->reservation['seat']

이것은 첫 공연에서 하나 명의 예약을 얻을 수 있지만, 나는 '3'예를 들어의 ID와 성능의 모든 예약을 원하는 .

FROM performances->performance['id=2'] SELECT reservations->reservation['seat'] 

이 XPath를 같이 공식화 할 수 있습니다 :

+0

가능한 중복 [SimpleXML을 : 특정 속성 값을 가질 요소 선택] (http://stackoverflow.com/questions/992450/simplexml-selecting-elements-which-have-a-certain-attribute-value) – hakre

답변

2

귀하의 "SQL"로

SELECT reservations->reservation['seat'] FROM performances->performance['id=2'] 

을 공식화 할 수 있습니다 당신이 필요로하는 무엇

 
//performances/performance[@id=2]/reservations/reservation/@seat 

개미. SimpleXML's xpath() method을보세요.

+0

FROM 공연을 썼지 만 특정 공연 ID 요소에서오고 싶다는 점을 강조하려고했습니다. PHP 만 사용하고 있습니다. – Henryz

+1

정확히 XPath가하는 일입니다. 특정 ID가있는 공연의 예약 만 선택합니다. * (PS : "PHP 만 사용하고 있습니다"는 무슨 뜻입니까?) * – Tomalak

+0

죄송합니다, 저는 정말 혼란 스럽습니다. ID가 7 인 요소 성능에서 3 예약 좌석 값을 가져올 PHP를 작성해야합니다. – Henryz

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'); 

?> 

Tomalak의 천둥을 훔쳐 와서 미안합니다. 어떤 사람들은 때로는 철자가 필요합니다. 의

+0

Hey Cory, 저는 XML과 XPATH에 익숙하지 않아서 개념을 이해하기가 어려웠습니다. xpath를 사용하여 (addAtrribute)를 쓸 수 있는지 알고 싶지 않습니까? – Henryz

관련 문제