用PHP解析XML中的两层名称空间
我一直试图从下面的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 内环 内环
提前致谢!
注册您自己的命名空间前缀。不要依赖文档中使用的名称空间前缀(和自动注册)。
$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;
}
}
}