DVCITIS
DVCITIS

Reputation: 1037

How do I reference an XML element with a namespace prefix using the object operator?

With regard to the below XML. How do I reference the children of <m:properties> using the object operator (->)?

$url = "http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData?$filter=month(NEW_DATE)%20eq%2011%20and%20year(NEW_DATE)%20eq%202015";
$xml = simplexml_load_file($url);
foreach( $xml->entry as $entry ) {
 $element = $xml->entry->content->properties->children();
}

$xml->entry->children(); works but $xml->entry->content->properties->children(); does not. I'm reading here that a colon (":") is placed between the namespace prefix and the element name/ attribute name so properties is therefore the element name, so not sure why not. This question is specifically concerned with using the object operator -> and children() function. I want to know why the same logic at different levels of the XML doc behaves differently; this differs to questions such as this that are looking for any solution to parsing XML data whether it be using the object operator -> and children() function or not.

<entry xmlns="http://www.w3.org/2005/Atom">
<id>
http://data.treasury.gov:8001/Feed.svc/DailyTreasuryYieldCurveRateData(6257)
</id>
<title type="text"/>
<updated>2015-11-15T13:40:16Z</updated>
<author>
<name/>
</author>
<link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(6257)"/>
<category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
<content type="application/xml">
<m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<d:Id xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Int32">6257</d:Id>
<d:NEW_DATE xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.DateTime">2015-01-02T00:00:00</d:NEW_DATE>
<d:BC_1MONTH xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">0.02</d:BC_1MONTH>
<d:BC_3MONTH xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">0.02</d:BC_3MONTH>
<d:BC_6MONTH xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">0.11</d:BC_6MONTH>
<d:BC_1YEAR xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">0.25</d:BC_1YEAR>
<d:BC_2YEAR xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">0.66</d:BC_2YEAR>
<d:BC_3YEAR xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">1.07</d:BC_3YEAR>
<d:BC_5YEAR xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">1.61</d:BC_5YEAR>


</m:properties>
</content>
</entry>

Upvotes: 0

Views: 80

Answers (1)

Jan
Jan

Reputation: 43169

You could use a solution with xpath and specify the namespace in the query:

$url = "http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData?$filter=month(NEW_DATE)%20eq%2011%20and%20year(NEW_DATE)%20eq%202015";
$xml = simplexml_load_file($url);

foreach ($xml->entry as $entry) { // loop over the entries
    print_r($entry->xpath('//d:BC_3MONTH')); // gives you the actual BC_3MONTH
    print_r($entry->xpath('//d:Id')); // the actual ID
}

Upvotes: 1

Related Questions