具有命名空间和 PHP 的相同嵌套 XML 元素 [英] Identical nested XML elements with namespaces and PHP

查看:27
本文介绍了具有命名空间和 PHP 的相同嵌套 XML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽我所能,我似乎无法在嵌套的 apcm:Property 元素中获取Id"属性的值,其中Name"属性等于sequenceNumber",在第 12 行.如您所见,有感兴趣的元素被埋在具有相同名称和命名空间的其他元素的嵌套中.

Try as I may, I cannot seem to grab the value of the "Id" attribute in the nested apcm:Property element, where the "Name" attribute equals "sequenceNumber", on line 12. As you can see, there element of interest is buried in a nest of other elements with an identical name and namespace.

使用 PHP,我很难思考如何获取该 Id 值.

Using PHP, I'm having a difficult time wrapping my head around how to grab that Id value.

<?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>

推荐答案

您必须注册命名空间,并使用 [] 谓词来确定您对哪个 Property 元素感兴趣.这是最安全的如果不使用双斜线,即从文档元素开始查找.

You have to register the namespaces, and use the [] predicate to identify which Property element you are interested in. It is safest if you do NOT use double slash, i.e., if you start the look up from the document element.

<?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 的同级 Property 元素,因此此 Xpath 可能会生成多个节点(@Id 值).

Note that there may theoretically be multiple sibling Property elements with the same @Name and so this Xpath may produce multiple nodes (@Id values).

这篇关于具有命名空间和 PHP 的相同嵌套 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆