0
我無法從這個XML得到以下信息命名空間:解析XML APFeeds使用PHP和SimpleXML
<apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:product:41664" Value="AP Top News" />
<apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:product:42430" Value="AP Top News - International - Stories" />
<apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:package:100518" Value="AP Top News Package" />
具體來說,我需要的「ID」和「價值」字段。
這裏是XML的主要部分:
<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:apcm="http://ap.org/schemas/03/2005/apcm" xmlns:apnm="http://ap.org/schemas/03/2005/apnm" xmlns:georss="http://www.georss.org/georss" xmlns:o="http://w3.org/ns/odrl/2/">
...
<entry xmlns="http://www.w3.org/2005/Atom">
...
<apcm:ContentMetadata xmlns:apcm="http://ap.org/schemas/03/2005/apcm">
...
<apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:product:41664" Value="AP Top News" />
<apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:product:42430" Value="AP Top News - International - Stories" />
<apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:package:100518" Value="AP Top News Package" />
...
</apcm:ContentMetadata>
</entry>
</feed>
我一直在看下面的SO職位,以揣摩出來,而這一次是最有幫助的,到目前爲止:Identical nested XML elements with namespaces and PHP
而這裏的是我與打碼:
$ns_dc = $feed_entry->children($ns['apcm']);
echo "APCM children: " . count($ns_dc) . "<br />";
$inner_ns_dc = $feed_entry->children($ns_dc["apcm:Property"]);
echo "APCM Property Children: " . count($inner_ns_dc) . "<br />";
//$sxe = new SimpleXMLElement($feed_entry);
$sxe = new SimpleXMLElement($feed_entry->asXML());
foreach($sxe->apcm as $item) {
printf("%s\n", $item);
}
$sxe->registerXPathNamespace('apcm', 'http://ap.org/schemas/03/2005/apcm');
$result = $sxe->xpath('/apcm:Property:*');
echo "Result count: " . count($result) . "<br />";
foreach ($result as $sequenceNumber) {
echo $sequenceNumber . "<br />";
}