DVCITIS
DVCITIS

Reputation: 1037

Retrieving XML values from namespace elements with PHP

Please could someone explain how to loop through the <entry> tags in this xml in order to echo all the elements with a d: prefix.

The below works, so I am on the right lines:

$url = "http://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/XmlView.aspx?data=yieldyear&year=2015";
$xml = simplexml_load_file($url);

foreach( $xml->entry as $entry ) {

   $element = $xml->entry->children();
        foreach( $element as $name=>$x ) {
             echo $name, '=', $x, "\n";
         }
}

But I'm struggling with the <content> element.

When i update $element to = $xml->entry->content->children();, it doesn't work.

Please could someone help me debug this?

Upvotes: 2

Views: 246

Answers (2)

ThW
ThW

Reputation: 19512

You're looking at this the wrong way. The actual namespace (http://schemas.microsoft.com/ado/2007/08/dataservices) is what you know. The prefix is specific for an element and its descendants - until it is redefined. It is optional for element nodes, too. The following tree elements all have the same namespace:

  • <f:foo xmlns:f="urn:foo"/>
  • <bar:foo xmlns:bar="urn:foo"/>
  • <foo xmlns="urn:foo"/>

Each of the elements resolves to a node with the name foo inside the namespace urn:foo. You can read it as {urn:foo}foo.

If you check the documentation for the methods, here are many that have the actual namespace an argument, so you do not have to know the prefix. For XPath you register your own prefix:

$url = "http://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/XmlView.aspx?data=yieldyear&year=2015";
$element = simplexml_load_file($url);

$element->registerXPathNamespace(
  'atom', 'http://www.w3.org/2005/Atom'
);
$element->registerXpathNamespace(
  'meta', 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata'
);

foreach ($element->xpath('//atom:entry/atom:content/meta:properties') as $properties) {
  $properties->registerXpathNamespace('data', 'http://schemas.microsoft.com/ado/2007/08/dataservices');
  echo $properties->xpath('data:Id')[0], "\n";
  echo $properties->xpath('data:NEW_DATE')[0], "\n\n";
}

Output:

6257
2015-01-02T00:00:00

6258
2015-01-05T00:00:00

6259
2015-01-06T00:00:00

6260
2015-01-07T00:00:00

6261
2015-01-08T00:00:00
...

You will have to register your prefixes on the element you like to call xpath() on. I used different prefixes in the example so you can see that you don't have to know the prefix - only the namespace itself.

In DOM it an separate object and DOMXpath:evaluate() can return scalar values directly.

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
$xpath->registerNamespace('meta', 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata');
$xpath->registerNamespace('data', 'http://schemas.microsoft.com/ado/2007/08/dataservices');

foreach ($xpath->evaluate('//atom:entry/atom:content/meta:properties') as $properties) {
  echo $xpath->evaluate('string(data:Id)', $properties), "\n";
  echo $xpath->evaluate('string(data:NEW_DATE)', $properties), "\n\n";
}

Upvotes: 2

Jan
Jan

Reputation: 43199

As in your other question, you could use an approach with xpath:

$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: 0

Related Questions