Getting Sub Elements with XPath

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all, 

I have the following XML returned to us.

<Response ResponseReference="REF_657-17000-305754723972091">
	<ResponseDetails Language="en">
		<SearchChargeConditionsResponse>
			<ChargeConditions>
				<ChargeCondition Type="cancellation">
					<Condition Charge="true" FromDay="0" ToDay="0" Currency="ZAR"
ChargeAmount="320.00"/>
					<Condition Charge="false" FromDay="1"/>
				</ChargeCondition>
				<ChargeCondition Type="amendment" MaximumPossibleChargesShown="true">
					<Condition Charge="false" FromDay="0"/>
				</ChargeCondition>
				<PassengerNameChange Allowable="true"/>
			</ChargeConditions>
		</SearchChargeConditionsResponse>
	</ResponseDetails>
</Response>

We need to get the values of the FromDay, ToDay and ChargeAmount from the
following sub elements: 

<Condition Charge="true" FromDay="0" ToDay="0" Currency="ZAR"
ChargeAmount="320.00"/>
<Condition Charge="false" FromDay="1"/>

I hve the following code: But I am not able to get the second element's
details: (Charge: false, FromDay: 1)

$Elements = $xpath->query(
'ResponseDetails/SearchChargeConditionsResponse/ChargeConditions/ChargeCondition/',
$responseElement );
				

				foreach( $Elements as $Element ) 
				{
					
					$itemStatus = $xpath->query('Condition/Charge' , $Element);
					$FromDay2 = $xpath->query('Condition/FromDay' , $Element);
					
					$condition = trim($itemStatus->item(0)->textContent);
					echo "<br /><br />CONDITION: #" . $condition."#";
					
					$FromDay = trim($FromDay2->item(0)->textContent);
					echo " FromDay: " . $FromDay;

					if ($condition =="true")
					{
						$ChargeAmount2 = $xpath->query('Condition/@ChargeAmount' , $Element);						
						$ChargeAmount = trim($ChargeAmount2->item(0)->textContent);
						echo "  CHARGE AMT: " . $ChargeAmount;

						$ToDay = $xpath->query('Condition/@ToDay' , $Element);
						$ToDay = trim($ToDay->item(0)->textContent);
						echo " ToDay: " . $ToDay;

					}
					else if($condition =="false")
					{
						$FromDay2 = $xpath->query('Condition/@FromDay' , $Element);
						$FromDay = trim($FromDay2->item(0)->textContent);
						echo " FromDay: " . $FromDay;
					}
				}					

It apprears that I need to add a further loop through the "ChargeCondition"
elements, IE

change 

$Elements = $xpath->query(
'ResponseDetails/SearchChargeConditionsResponse/ChargeConditions/ChargeCondition/',
$responseElement );


to

$Elements = $xpath->query(
'ResponseDetails/SearchChargeConditionsResponse/ChargeConditions/',
$responseElement );

Then I can test the type if its "cancellation" and loop through the
subelements (Condition)

Would this appear to be correct?

Anything else that would appear to be incorrect from the above code for the
accompanying structure?

thanks
angelo

-- 
View this message in context: http://www.nabble.com/Getting-Sub-Elements-with-XPath-tp23312506p23312506.html
Sent from the PHP - General mailing list archive at Nabble.com.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux