仅当 xpath 中有默认命名空间时才能选择第一个节点 [英] Only able to select first node when there is a default namespace in xpath

查看:24
本文介绍了仅当 xpath 中有默认命名空间时才能选择第一个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查询包含默认命名空间的 xml 文件时遇到问题,这是一场噩梦.

I've been having problems querying an xml file that contains default namespaces and it's been a nightmare.

我已经能够在声明命名空间后选择第一个节点,但后面的任何内容都会被忽略.

I've been able to select the first node after declaring a namespace but anything that follows gets ignored.

$str = '<?xml version="1.0"?>
<GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
    <GetLowestOfferListingsForASINResult>
      <Product>
        <LowestOfferListings>
          <LowestOfferListing>
            <Qualifiers>
              <ItemCondition>Used</ItemCondition>
              <ItemSubcondition>Good</ItemSubcondition>
              <FulfillmentChannel>Merchant</FulfillmentChannel>
              <ShipsDomestically>Unknown</ShipsDomestically>
              <ShippingTime>
                <Max>0-2 days</Max>
              </ShippingTime>
              <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
            </Qualifiers>
            <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
            <SellerFeedbackCount>83352</SellerFeedbackCount>
            <Price>
              <LandedPrice>
                <CurrencyCode>GBP</CurrencyCode>
                <Amount>7.40</Amount>
              </LandedPrice>
              <ListingPrice>
                <CurrencyCode>GBP</CurrencyCode>
                <Amount>4.60</Amount>
              </ListingPrice>
              <Shipping>
                <CurrencyCode>GBP</CurrencyCode>
                <Amount>2.80</Amount>
              </Shipping>
            </Price>
            <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
          </LowestOfferListing>
        </LowestOfferListings>
      </Product>
    </GetLowestOfferListingsForASINResult>
</GetLowestOfferListingsForASINResponse>';

$xml = new SimpleXMLElement($str);
$xml->registerXPathNamespace('c', 'http://mws.amazonservices.com/schema/Products/2011-10-01');
print_r($xml->xpath("c:GetLowestOfferListingsForASINResult"));          //works
print_r($xml->xpath("c:GetLowestOfferListingsForASINResult/Product"));  //not working
print_r($xml->xpath("c:GetLowestOfferListingsForASINResult//Product")); //not working

推荐答案

Xpath 没有自动默认命名空间(它总是 null-namespace),因此您需要显式使用带有 all 元素名称的c:"前缀:

Xpath has no automatic default namespace (it's always the null-namespace), so you need to explicitly use the "c:" prefix with all element names:

c:GetLowestOfferListingsForASINResult/c:Product
                                      ^^

c:GetLowestOfferListingsForASINResult//c:Product
                                       ^^

如果您对这种情况的详细信息感兴趣,本网站上有以下现有问答:

if you're interested in the details why this is the case, there is the following existing Q&A on this website:

这篇关于仅当 xpath 中有默认命名空间时才能选择第一个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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