Retrieving XML values from namespace elements with PHP

前端 未结 2 750
一整个雨季
一整个雨季 2020-12-21 15:13

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

相关标签:
2条回答
  • 2020-12-21 15:18

    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";
    }
    
    0 讨论(0)
  • 2020-12-21 15:38

    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
    }
    
    0 讨论(0)
提交回复
热议问题