1

試してみても、12 行目の「Name」属性が「sequenceNumber」に等しい、ネストされた apcm:Property 要素の「Id」属性の値を取得できないようです。ご覧のとおり、関心のある要素があります。同じ名前と名前空間を持つ他の要素のネストに埋もれています。

PHP を使用して、その Id 値を取得する方法に頭を悩ませています。

<?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">
    <id>urn:publicid:ap.org:30085</id>
<title type="xhtml">
    <apxh:div xmlns:apxh="http://www.w3.org/1999/xhtml">
        <apxh:span>AP New York State News - No Weather</apxh:span>
    </apxh:div>
</title>
<apcm:Property Name="FeedProperties">
    <apcm:Property Name="Entitlement" Id="urn:publicid:ap.org:product:30085" Value="AP New York State News - No Weather" />
    <apcm:Property Name="FeedSequencing">
            <apcm:Property Name="sequenceNumber" Id="169310964" />
            <apcm:Property Name="minDateTime" Value="2012-05-22T18:04:18.913Z" />
    </apcm:Property>
</apcm:Property>
<updated>2012-05-22T18:04:18.913Z</updated>
<author>
    <name>The Associated Press</name>
    <uri>http://www.ap.org</uri>
</author>
<rights>Copyright 2012 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.</rights>
<link rel="self" href="http://syndication.ap.org/AP.Distro.Feed/GetFeed.aspx?idList=30085&amp;idListType=products&amp;maxItems=20" />
<entry>
...
</entry>
</feed>
4

1 に答える 1

0

名前空間を登録し、[]述語を使用して関心のある Property 要素を識別する必要があります。二重スラッシュを使用しない場合、つまりドキュメント要素から検索を開始する場合が最も安全です。

<?php

$xml = <<<EOD
...
EOD;

$sxe = new SimpleXMLElement($xml);

$sxe->registerXPathNamespace('apcm', 'http://ap.org/schemas/03/2005/apcm');
$sxe->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom');

$result = $sxe->xpath('/atom:feed/acpm:Property[@Name=\'FeedProperties\']/acpm:Property[@Name=\'FeedSequencing\']/acpm:Property[@Name=\'sequenceNumber\']/@Id');

foreach ($result as $sequenceNumber) {
  echo $sequenceNumber . "\n";
}

?>

理論的には、複数の兄弟 Property 要素が同じ@Nameである可能性があるため、この Xpath は複数のノード (@Id値) を生成する可能性があることに注意してください。

于 2012-05-23T20:17:50.513 に答える