1

我一直在尝试从以下 XML 文档中解析标题和描述(这只是顶部)。我找到了很多关于处理命名空间的信息,但我不知道如何处理涉及获取使用命名空间的内容的情况,该命名空间是具有命名空间的其他东西的子项。任何帮助都会很棒!

<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>

我一直在尝试使用的代码:本质上,我一直在尝试做的是使用嵌套的 foreach 循环来获取 ['oai_dc'] 的孩子的孩子。

<?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;


  }
}

?>

这是示例输出(同样,上面列出的 XML 只是部分的,完整的 XML 太长了):

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

提前致谢!

4

1 回答 1

0

注册您自己的命名空间前缀。不要依赖文档中使用的命名空间前缀(以及自动注册)。

$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;
    }
  }
}
于 2013-11-21T09:57:23.490 回答