gpt4 book ai didi

php - 在php中使用Xpath,返回一个空数组

转载 作者:行者123 更新时间:2023-12-03 17:36:38 27 4
gpt4 key购买 nike

大家好,我正在尝试从这个 XML 中获取所有艺术家:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<metadata created="2015-02-03T04:03:18.468Z"
xmlns="http://musicbrainz.org/ns/mmd-2.0#"
xmlns:ext="http://musicbrainz.org/ns/ext#-2.0">
<artist-list count="6310" offset="0">
<artist id="d51955f3-df06-432c-a90d-0308f3c48273" type="Person" ext:score="100">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<gender>male</gender>
<country>US</country>
<area id="489ce91b-6658-3307-9877-795b68554c98">
<name>United States</name>
<sort-name>United States</sort-name>
</area>
<begin-area id="82f3a697-ba65-404d-a1ed-360147af7d10">
<name>San Diego</name>
<sort-name>San Diego</sort-name>
</begin-area>
<end-area id="ae0110b6-13d4-4998-9116-5b926287aa23">
<name>California</name>
<sort-name>California</sort-name>
</end-area>
<disambiguation>jazz pianist, composer, arranger and bandleader</disambiguation>
<life-span>
<begin>1922-04-17</begin>
<end>2013-06-29</end>
<ended>true</ended>
</life-span>
<alias-list>
<alias sort-name="Smith, Paul T.">Paul T. Smith</alias>
</alias-list>
</artist>
<artist id="443ead81-02ce-4136-a759-71453184c002" type="Person" ext:score="100">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<gender>male</gender>
<country>US</country>
<area id="489ce91b-6658-3307-9877-795b68554c98">
<name>United States</name>
<sort-name>United States</sort-name>
</area>
<begin-area id="f83bde89-c84a-4846-a89a-d5d0874e9ddc">
<name>Calumet</name>
<sort-name>Calumet</sort-name>
</begin-area>
<end-area id="6db2e45d-d7f3-43da-ac0b-7ba5ca627373">
<name>Glendale</name>
<sort-name>Glendale</sort-name>
</end-area>
<disambiguation>film and television composer, Disney Legends honoree</disambiguation>
<life-span>
<begin>1906-10-30</begin>
<end>1985-01-25</end>
<ended>true</ended>
</life-span>
<alias-list>
<alias sort-name="Smith, Paul J.">Paul J. Smith</alias>
</alias-list>
</artist>
<artist id="fac3e6c4-6d3c-4086-a590-95e4a057ac42" ext:score="95">
<name>Paul Smith</name>
<sort-name>Paul Smith</sort-name>
<disambiguation>Banjo</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="8ffc42c7-4083-4435-b91c-6ddf4095eeb3" ext:score="95">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<country>US</country>
<area id="489ce91b-6658-3307-9877-795b68554c98">
<name>United States</name>
<sort-name>United States</sort-name>
</area>
<disambiguation>American soul artist</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="2f727018-1e83-4797-8433-32c8b99dd057" type="Person" ext:score="94">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<gender>male</gender>
<country>GB</country>
<area id="8a754a16-0027-3a29-b6d7-2b40ea0481ed">
<name>United Kingdom</name>
<sort-name>United Kingdom</sort-name>
</area>
<disambiguation>of Maxïmo Park</disambiguation>
<life-span>
<begin>1979-03-13</begin>
<ended>false</ended>
</life-span>
</artist>
<artist id="e44ae2fa-9367-4691-bdb7-d097446ee3ce" type="Person" ext:score="94">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<disambiguation>Gabba/Acid techno artist</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="32c7cf45-867e-4076-a24c-ba38dccaa7bd" type="Person" ext:score="94">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<country>US</country>
<area id="489ce91b-6658-3307-9877-795b68554c98">
<name>United States</name>
<sort-name>United States</sort-name>
</area>
<disambiguation>American folk artist</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="bb57b210-bf27-4af1-8561-600f131bfc81" type="Person" ext:score="94">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<disambiguation>drum n bass producer aka Spinback</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="35287ce6-2fd4-41af-9248-d07e84eeb0df" type="Person" ext:score="94">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<disambiguation>Contemporary Christian artist peformed with The Imperials</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="6ece7fa3-c397-48a6-99f7-05e900b73d81" type="Person" ext:score="82">
<name>Nicholas Paul Smith</name>
<sort-name>Smith, Nicholas Paul</sort-name>
<gender>male</gender>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="3c1fbcf7-4bb8-49ac-8d35-3f4ee8b71e0a" type="Person" ext:score="79">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<gender>male</gender>
<country>US</country>
<area id="489ce91b-6658-3307-9877-795b68554c98">
<name>United States</name>
<sort-name>United States</sort-name>
</area>
<disambiguation>recording engineer, David "Honeyboy" Edwards recordings</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="24e472f9-6003-4873-aac5-c79763105a51" type="Person" ext:score="79">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<gender>male</gender>
<country>GB</country>
<area id="8a754a16-0027-3a29-b6d7-2b40ea0481ed">
<name>United Kingdom</name>
<sort-name>United Kingdom</sort-name>
</area>
<disambiguation>UK drummer</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="662c9cd6-73e2-426f-8e76-a8f2a8233215" type="Person" ext:score="79">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<country>AU</country>
<area id="106e0bec-b638-3b37-b731-f53d507dc00e">
<name>Australia</name>
<sort-name>Australia</sort-name>
</area>
<disambiguation>Australian bassist for Laughing Clowns/Ed Kuepper</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="0e911646-290a-4345-b3dc-e5ca91faaf88" type="Person" ext:score="79">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<gender>male</gender>
<country>GB</country>
<area id="8a754a16-0027-3a29-b6d7-2b40ea0481ed">
<name>United Kingdom</name>
<sort-name>United Kingdom</sort-name>
</area>
<disambiguation>British a cappella singer</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="75ffe808-187a-4488-8c84-7467efa2b277" type="Person" ext:score="79">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<disambiguation>of Dengue Fever</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="79d145cb-d862-487d-a340-b723a7c8eb26" type="Person" ext:score="79">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<country>GB</country>
<area id="8a754a16-0027-3a29-b6d7-2b40ea0481ed">
<name>United Kingdom</name>
<sort-name>United Kingdom</sort-name>
</area>
<disambiguation>Scottish electronic artist performing as Reverbaphon</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="1f483d17-7510-44d1-95a7-8c16c53c57a1" type="Person" ext:score="79">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<country>US</country>
<area id="489ce91b-6658-3307-9877-795b68554c98">
<name>United States</name>
<sort-name>United States</sort-name>
</area>
<disambiguation>US underground hip hop MC Breeze Brewin, of Weatherman, Def Jux</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="76e04e2a-e440-4729-8036-59928521c677" type="Person" ext:score="79">
<name>Paul Smith</name>
<sort-name>Smith, Paul</sort-name>
<gender>male</gender>
<country>US</country>
<area id="489ce91b-6658-3307-9877-795b68554c98">
<name>United States</name>
<sort-name>United States</sort-name>
</area>
<disambiguation>sound engineer, Randy Crawford &amp; Joe Sample recordings</disambiguation>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="7a81a64e-4fdc-44a8-b33b-a447c611cf33" type="Group" ext:score="76">
<name>The Paul Smith Trio</name>
<sort-name>Paul Smith Trio, The</sort-name>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="7b458c24-ce5b-4b77-a1dc-616ff53d1e70" type="Group" ext:score="76">
<name>The Paul Smith Quartet</name>
<sort-name>Paul Smith Quartet, The</sort-name>
<country>US</country>
<area id="489ce91b-6658-3307-9877-795b68554c98">
<name>United States</name>
<sort-name>United States</sort-name>
</area>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="1c86fcf3-b56c-4427-856b-344d898abc33" ext:score="76">
<name>Paul Gerard Smith</name>
<sort-name>Smith, Paul Gerard</sort-name>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="e5fcb860-3210-4721-a44f-7bd3dcb4b5d5" ext:score="76">
<name>Paul Joseph Smith</name>
<sort-name>Paul Joseph Smith</sort-name>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="3eedd38b-f7ca-43d2-b635-adc4ad95371b" type="Person" ext:score="74">
<name>Margaret Thatcher</name>
<sort-name>Thatcher, Margaret</sort-name>
<gender>female</gender>
<country>GB</country>
<area id="8a754a16-0027-3a29-b6d7-2b40ea0481ed">
<name>United Kingdom</name>
<sort-name>United Kingdom</sort-name>
</area>
<life-span>
<begin>1925-10-13</begin>
<end>2013-04-08</end>
<ended>true</ended>
</life-span>
<tag-list>
<tag count="1">
<name>politician</name>
</tag>
<tag count="1">
<name>non-musician</name>
</tag>
</tag-list>
</artist>
<artist id="646c86db-d0af-403e-bef9-3deafd9c3d43" type="Person" ext:score="74">
<name>Brad Thatcher</name>
<sort-name>Thatcher, Brad</sort-name>
<life-span>
<ended>false</ended>
</life-span>
</artist>
<artist id="b5425c52-3e9c-4144-9467-eaa4d69389b8" type="Person" ext:score="74">
<name>Jim Thatcher</name>
<sort-name>Thatcher, Jim</sort-name>
<life-span>
<ended>false</ended>
</life-span>
<alias-list>
<alias sort-name="Thatcher, James" type="Legal name">James Thatcher</alias>
</alias-list>
</artist>
</artist-list>
</metadata>

但我得到的只是一个空数组。

这是我的代码:
<!doctype html>
<html>
<head>
<body>
<meta charset="utf-8">
<title>XML: Descrizione, Immagine, Link a Discogs</title>
</head>
<body>
<?php
$bb = "Paul Thatcher Smith"; //Example.
$name = strtolower(str_replace(" ","%20",$bb)); //all whitespaces must be coded with %20
$html = "http://musicbrainz.org/ws/2/artist/?query=".$name;

if (($response_xml_data = file_get_contents($html))===false){
echo "Error fetching XML\n";
}
else {
libxml_use_internal_errors(true);
$data = new SimpleXmlElement($response_xml_data); //all ok, you can do an echo, xml is well formed
$data->registerXPathNamespace('c',$html); //according to w3s
if (!$data) { //always have this error -.-
echo "Error loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
}
else {
$xpath='//c:artist'; //simple xpath query, if i put //* it gives me all xml
print_r($result = $data->xpath($xpath));
foreach ($result as $artist){
echo $artist . "<br>";
}
}
}
?>

最佳答案

此行不正确:

$data->registerXPathNamespace('c',$html); 
$html是您正在查询的 URL,但在这一行中,您需要指定您正在使用的 XML 的命名空间。该命名空间是:
http://musicbrainz.org/ns/mmd-2.0#

所以使用这个:
$data->registerXPathNamespace('c','http://musicbrainz.org/ns/mmd-2.0#'); 

关于php - 在php中使用Xpath,返回一个空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28281796/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com