【问题标题】:Find tags of SimpleXMLElement using xpath [duplicate]使用 xpath 查找 SimpleXMLElement 的标签 [重复]
【发布时间】:2014-08-25 04:17:54
【问题描述】:

我尝试获取以下 XML 文件的所有 BrowseNodeId:

<?xml version="1.0" ?>
<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2013-08-01">
<OperationRequest>
    <RequestId>00ffd0c4-65b8-40ed-96b3-dbee109c508e</RequestId>
    <Arguments>
        <Argument Name="Condition" Value="All"></Argument>
        <Argument Name="region" Value="com"></Argument>
    </Arguments>
    <RequestProcessingTime>0.0428190000000000</RequestProcessingTime>
</OperationRequest>
<Items>
    <Request>
        <IsValid>True</IsValid>
        <ItemSearchRequest>
            <Condition>All</Condition>
            <Keywords>0321185587</Keywords>
            <ResponseGroup>BrowseNodes</ResponseGroup>
            <SearchIndex>Books</SearchIndex>
        </ItemSearchRequest>
    </Request>
    <TotalResults>1</TotalResults>
    <TotalPages>1</TotalPages>
    <Item>
    <ASIN>0321185587</ASIN>
    <BrowseNodes>
        <BrowseNode>
            <BrowseNodeId>21</BrowseNodeId>
            <Name>Reference</Name>
                <Children>
                    <BrowseNode>
                        <BrowseNodeId>11444</BrowseNodeId>
                        <Name>Almanacs &amp; Yearbooks</Name>
                    </BrowseNode>
                    <BrowseNode>
                        <BrowseNodeId>11448</BrowseNodeId>
                        <Name>Atlases &amp; Maps</Name>
                    </BrowseNode>
                    <BrowseNode>
                        <BrowseNodeId>2572</BrowseNodeId>
                        <Name>Careers</Name>
                    </BrowseNode>
                </Children>
        </BrowseNode>
    </BrowseNodes>
    </Item>
</Items>
</ItemSearchResponse>

这是我使用的 PHP 代码:

$xml= simplexml_load_file($xml_file);
foreach ($xml->xpath('//BrowseNode') as $node) {
      echo $node->BrowseNodeId.":".$node->Name."<br>";
}

但结果为 NULL。我不擅长 XML,所以我不知道我错在哪里。谁能告诉我这个案例的正确 xpath 语法吗?

【问题讨论】:

    标签: php xml


    【解决方案1】:

    这是因为您的文档在 NULL 命名空间中没有名为 BrowseNode 的节点。

    取而代之的是在 http://webservices.amazon.com/AWSECommerceService/2013-08-01 命名空间中有名为 BrowseNode 的节点。

    要通过 xpath 访问这些节点,您需要先为该命名空间注册一个前缀:

    $xml->registerXPathNamespace(
        "prefix", 
        "http://webservices.amazon.com/AWSECommerceService/2013-08-01"
    );
    

    然后您可以在 xpath 查询中使用该 前缀

    foreach ($xml->xpath('//prefix:BrowseNode') as $node) {
        echo $node->BrowseNodeId, ":", $node->Name, "\n";
    }
    

    (你当然可以给那个前缀起一个不同的名字,这样它会说更多,也许是“awsecs”,但这取决于你)。

    你会得到预期的结果:

    21:Reference
    11444:Almanacs & Yearbooks
    11448:Atlases & Maps
    2572:Careers
    

    这已经在xPath finds nothing but * 和类似的地方得到了回答。

    【讨论】:

    • 效果很好。谢谢你,@hakre。我总是在询问之前搜索stackoverflow。关键是我不知道我错在哪里,所以我找不到相关的答案。
    • xmlns 被称为 XMLS 命名空间——每个以 XML 开头的属性或元素都是特殊的。
    • 感谢您对我的帮助很大!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 2018-04-29
    • 2013-07-05
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多