鉴于下面从 W3C网站获取的 XML/RDF 示例,我如何访问“cd”命名空间中的值?
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cd="http://www.recshop.fake/cd#">
<rdf:Description
rdf:about="http://www.recshop.fake/cd/Empire Burlesque">
<cd:artist>Bob Dylan</cd:artist>
<cd:country>USA</cd:country>
<cd:company>Columbia</cd:company>
<cd:price>10.90</cd:price>
<cd:year>1985</cd:year>
</rdf:Description>
</rdf:RDF>
我尝试过执行以下操作:
$XML = new SimpleXMLElement($rawXML); // Assume $rawXML is the quoted XML/RDF above
foreach($xml as $entry){
$cd = $entry->children('http://www.recshop.fake/cd#');
echo $cd->artist;
echo $cd->$country;
...
}
我也尝试过:
$XML = new SimpleXMLElement($rawXML); // Assume $rawXML is the quoted XML/RDF above
foreach($xml as $entry){
$cd = $entry->children('http://www.recshop.fake/cd#');
$rdf = $entry->children('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
echo $rdf->$cd->artist;
echo $rdf->$cd->$country;
...
}
此外,在 PHP 中是否有必要做任何不同的事情,而不是声明xmlns:cd="http://www.recshop.fake/cd#"
它是xmlns="http://www.recshop.fake/cd#"
并且“cd”命名空间已从<cd:artist>
等中删除。