Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有名称空间和PHP的相同嵌套XML元素_Php_Xml - Fatal编程技术网

具有名称空间和PHP的相同嵌套XML元素

具有名称空间和PHP的相同嵌套XML元素,php,xml,Php,Xml,尽管我可以尝试,但我似乎无法在嵌套的apcm:Property元素中获取“Id”属性的值,其中第12行的“Name”属性等于“sequenceNumber”。如您所见,感兴趣的元素被隐藏在具有相同名称和命名空间的其他元素的嵌套中 使用PHP,我很难理解如何获取Id值 <?xml version="1.0" encoding="utf-8" ?> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:apcm="http://ap.org

尽管我可以尝试,但我似乎无法在嵌套的apcm:Property元素中获取“Id”属性的值,其中第12行的“Name”属性等于“sequenceNumber”。如您所见,感兴趣的元素被隐藏在具有相同名称和命名空间的其他元素的嵌套中

使用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>

urn:publicid:ap.org:30085
美联社纽约州新闻-无天气
2012-05-22T18:04:18.913Z
美联社
http://www.ap.org
美联社版权所有。版权所有。本材料不得出版、广播、重写或重新分发。
...

您必须注册名称空间,并使用
[]
谓词来标识您感兴趣的属性元素。如果不使用双斜杠,即从文档元素开始查找,则最安全

<?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";
}

?>

请注意,理论上可能有多个同级属性元素具有相同的
@Name
,因此此Xpath可能会生成多个节点(
@Id
值)