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?

1
  • 1
    this topic has been tackled before you can use search to check out some answers Commented Nov 16, 2015 at 6:50

2 Answers 2

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";
}
Sign up to request clarification or add additional context in comments.

2 Comments

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: stackoverflow.com/questions/33809590/…
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.
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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.