1

I have been trying to parse the the title and description from the following XML document (this is just the top part). I have found lots of information about dealing with namespaces but I cannot figure out how to deal with a situation involving getting the content of something that uses names spaces that is the child of something else with a namespace. Any help would be amazing!

<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>2013-10-04T23:19:36Z</responseDate>
<request verb="ListRecords">https://digital.grinnell.edu/drupal/oai2</request>
<ListRecords>
<record>
<header>
<identifier>oai:digital.grinnell.edu:grinnell_166</identifier>
<datestamp>2013-07-02T20:59:30Z</datestamp>
</header>
<metadata>
<oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
<dc:title xmlns:dc="http://purl.org/dc/elements/1.1/">
Grinnell College Young Innovator for Social Justice Prize Award Ceremony, 2011
</dc:title>
<dc:title xmlns:dc="http://purl.org/dc/elements/1.1/">
Young Innovator for Social Justice Prize Award Ceremony, 2011
</dc:title>
<dc:type xmlns:dc="http://purl.org/dc/elements/1.1/">Video</dc:type>
<dc:description xmlns:dc="http://purl.org/dc/elements/1.1/">
An edited recording of the award ceremony for the first Grinnell College Young Innovator for Social Justice Prize.
</dc:description>

The code I have been trying to use for this: Essentially, what I've been trying to do is get the children of the children of the ['oai_dc'] using nested foreach loops.

<?php
$feed = file_get_contents("https://digital.grinnell.edu/drupal/oai2?verb=ListRecords&metadataPrefix=oai_dc&from=2013-06-28&set=islandora_genericCollection");
$xml = new SimpleXmlElement($feed);
foreach ($xml->ListRecords->record as $entry){
  echo $entry->header->identifier;
  echo "<br>";
  echo $entry->header->datestamp;
  echo "<br>";
  //Use that namespace

  $dc = $entry->children($namespaces['oai_dc']);
  foreach ($dc as $dcElement){
    $childern = $dcElement->children($namespaces['dc'])->title;
    echo $children;
    echo "inner loop";
    echo $children->title;
    echo "<br>";
    echo $dcElement->description;


  }
}

?>

Here is sample output (again the XML listed above is only partial the full XML is too long):

oai:digital.grinnell.edu:grinnell_166 2013-07-02T20:59:30Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3268 2013-07-02T23:16:17Z inner loop inner loop oai:digital.grinnell.edu:grinnell_209 2013-07-01T16:36:55Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3269 2013-07-05T23:16:18Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3246 2013-07-17T19:21:41Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3326 2013-07-18T13:22:46Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3249 2013-07-18T13:30:02Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3324 2013-07-18T13:31:01Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3327 2013-07-18T23:16:01Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3328 2013-07-18T23:16:02Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3329 2013-07-18T23:16:02Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3300 2013-08-07T19:28:45Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3256 2013-08-07T19:32:07Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3276 2013-08-07T19:39:05Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3279 2013-08-07T19:42:51Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3281 2013-08-07T19:44:55Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3293 2013-08-07T19:47:21Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3277 2013-08-07T19:50:29Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3257 2013-08-07T19:52:32Z inner loop inner loop oai:digital.grinnell.edu:grinnell_3289 2013-08-07T19:54:31Z inner loop inner loop

Thanks in advance!

Bren
  • 3,516
  • 11
  • 41
  • 73

1 Answers1

0

Register your own namespace prefixes. Do not rely on the namespaces prefixes used in the document (and automatic registration).

$dom = new DOMDocument();
$dom->load('https://digital.grinnell.edu/drupal/oai2?verb=ListRecords&metadataPrefix=oai_dc&from=2013-06-28&set=islandora_genericCollection');
$xpath = new DOMXpath($dom);

$xpath->registerNamespace('oai', 'http://www.openarchives.org/OAI/2.0/');
$xpath->registerNamespace('oai-dc', 'http://www.openarchives.org/OAI/2.0/oai_dc/');
$xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');

$records = $xpath->evaluate('/oai:OAI-PMH/oai:ListRecords/oai:record', NULL, FALSE);
foreach ($records as $entry) {
  echo $xpath->evaluate('string(oai:header/oai:identifier)', $entry, FALSE);
  echo "<br>";
  echo $xpath->evaluate('string(oai:header/oai:datestamp)', $entry, FALSE);
  echo "<br>";

  $dc = $xpath->evaluate('oai:metadata/oai-dc:dc/dc:*', $entry, FALSE);
  foreach ($dc as $dcElement){
    switch ($dcElement->localName) {
    case 'title' :
      echo 'Title: '.$dcElement->nodeValue;
      echo "<br>";
      break;
    case 'description' :
      echo 'Description: '.$dcElement->nodeValue;
      echo "<br>";
      break;
    }
  }
}
ThW
  • 19,120
  • 3
  • 22
  • 44