2013-10-05 100 views
1

我一直試圖從下面的XML文檔(這只是最上面的部分)解析標題和描述。我發現了很多關於處理命名空間的信息,但我無法弄清楚如何處理涉及使用命名空間使用名稱空間的東西的內容的情況。任何幫助將是驚人的!用PHP解析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埃杜:grinnell_3246 2013-07-17T19:21:41Z 內環 內環 OAI:digital.grinnell.edu:grinnell_3326 2013-07-18T13:22:46Z 內環 內環 OAI:數字.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:數字。 grinnell.edu:grinnell_3257 2013-08-07T19:52:32Z 內環 內環 OAI:digital.grinnell.edu:grinnell_3289 2013-08-07T19:54:31Z 內環 內環

提前致謝!

回答

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