2

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?

DVCITIS
  • 1,067
  • 3
  • 16
  • 36
  • 1
    this topic has been tackled [before](http://stackoverflow.com/questions/595946/parse-xml-with-namespace-using-simplexml) you can use search to check out some answers – Kevin Nov 16 '15 at 06:50

2 Answers2

2

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";
}
ThW
  • 19,120
  • 3
  • 22
  • 44
  • Great answer thanks. I jumped in at the deepend so taking a look at prefixes/namespaces in more detail. I have a follow on Q for you! Would //atom:entry/atom:content/m:properties also work in the foreach line? I've created a new question on this: http://stackoverflow.com/questions/33809590/using-xml-prefixes-namespaces-to-create-an-xpath – DVCITIS Nov 19 '15 at 16:41
  • You already got the right answer for that question - I upvoted it. Both the document and the Xpath engine use prefixes for the namespaces. Both resolve them internally to the namespace and use the namespace uri. Some libraries automatically register the namespace of the context node, but this is fragile, just register your own. – ThW Nov 20 '15 at 00:02
0

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
}
Community
  • 1
  • 1
Jan
  • 42,290
  • 8
  • 54
  • 79