使用 PHP 和 SimpleXML 解析带有命名空间的 APFeeds XML

Parsing APFeeds XML with namespaces using PHP and SimpleXML

我无法从此 XML 获取以下信息:

<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" 和 "Value" 字段。

这是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 />";
    }

我想你可以注册命名空间,然后使用这个 xpath 表达式来获取元素:

$elements = $feed_entry->xpath('//a:entry/apcm:ContentMetadata/apcm:Property');

$elementsSimpleXMLElement objects from which you can get the attributes的数组。

$source = <<<SOURCE
<?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>
SOURCE;

$feed_entry = simplexml_load_string($source);
$feed_entry->registerXPathNamespace('a', 'http://www.w3.org/2005/Atom');
$elements = $feed_entry->xpath('//a:entry/apcm:ContentMetadata/apcm:Property');

foreach ($elements as $element) {
    $id = $element->attributes()->Id->__toString();
    $value = $element->attributes()->Value->__toString();

    echo "The Id is: $id and the Value is: $value<br>";
}

将导致:

The Id is: urn:publicid:ap.org:product:41664 and the Value is: AP Top News

The Id is: urn:publicid:ap.org:product:42430 and the Value is: AP Top News - International - Stories

The Id is: urn:publicid:ap.org:package:100518 and the Value is: AP Top News Package

Demo