元素树 - 在不循环的情况下搜索特定元素值

Element Tree - Seaching for specific element value without looping

我有一个 xml 文件,其中包含一堆 xml 的重复块。我只想在报价具有特定 SellerId 且我的商品具有 SubCondtion='New' 时提取标价。我现在可以通过遍历报价并根据我的卖家 ID 检查 xml 块中的 OfferSellerId 并确保 Subcondition = 'New' 但我想知道是否有办法抓住ListingPrice 使用 findfindall 检查同一 <Offer> 块中的 SellerIdSubCondition 是否仅使用元素树逻辑具有这些特定值而不是对每个报价进行循环。

当前代码

for offer in root.findall('./NotificationPayload/AnyOfferChangedNotification/Offers/'):
        OfferSellerId = offer.find('SellerId').text
        SubCondition = offer.find('SubCondition').text

        if (SellerId == OfferSellerId && SubCondition == 'New'):
          ListingPrice  = offer.find('ListingPrice/Amount').text 

xml

  .......
  <Offers>
        <Offer>
            <SellerId>A2LZYV</SellerId>
            <SubCondition>new</SubCondition>
            <SellerFeedbackRating>
                <SellerPositiveFeedbackRating>100</SellerPositiveFeedbackRating>
                <FeedbackCount>929</FeedbackCount>
            </SellerFeedbackRating>
            <ShippingTime minimumHours="24" maximumHours="48" availabilityType="NOW"/>
            <ListingPrice>
                <Amount>16.69</Amount>
                <CurrencyCode>USD</CurrencyCode>
            </ListingPrice>
            <Shipping>
                <Amount>0.00</Amount>
                <CurrencyCode>USD</CurrencyCode>
            </Shipping>
            <ShipsFrom>
                <Country>US</Country>
                <State>FL</State>
            </ShipsFrom>
            <IsFulfilledByAmazon>false</IsFulfilledByAmazon>
            <IsBuyBoxWinner>false</IsBuyBoxWinner>
            <PrimeInformation>
                <IsPrime>false</IsPrime>
                <IsNationalPrime>false</IsNationalPrime>
            </PrimeInformation>
            <IsFeaturedMerchant>true</IsFeaturedMerchant>
            <ShipsDomestically>true</ShipsDomestically>
        </Offer>
        ......

即使ElementTree has limited XPath support,也应该足够做你想做的了...

XML 输入 (test.xml)

<doc>
    <NotificationPayload>
        <AnyOfferChangedNotification>
            <Offers>
                <Offer>
                    <SellerId>A2LZYV</SellerId>
                    <SubCondition>new</SubCondition>
                    <SellerFeedbackRating>
                        <SellerPositiveFeedbackRating>100</SellerPositiveFeedbackRating>
                        <FeedbackCount>929</FeedbackCount>
                    </SellerFeedbackRating>
                    <ShippingTime minimumHours="24" maximumHours="48" availabilityType="NOW"/>
                    <ListingPrice>
                        <Amount>16.69</Amount>
                        <CurrencyCode>USD</CurrencyCode>
                    </ListingPrice>
                    <Shipping>
                        <Amount>0.00</Amount>
                        <CurrencyCode>USD</CurrencyCode>
                    </Shipping>
                    <ShipsFrom>
                        <Country>US</Country>
                        <State>FL</State>
                    </ShipsFrom>
                    <IsFulfilledByAmazon>false</IsFulfilledByAmazon>
                    <IsBuyBoxWinner>false</IsBuyBoxWinner>
                    <PrimeInformation>
                        <IsPrime>false</IsPrime>
                        <IsNationalPrime>false</IsNationalPrime>
                    </PrimeInformation>
                    <IsFeaturedMerchant>true</IsFeaturedMerchant>
                    <ShipsDomestically>true</ShipsDomestically>
                </Offer>
            </Offers>            
        </AnyOfferChangedNotification>
    </NotificationPayload>
</doc>

Python

import xml.etree.ElementTree as ET

root = ET.parse("test.xml")

SellerId = "A2LZYV"

for price in root.findall(f".//Offer[SellerId='{SellerId}'][SubCondition='new']/ListingPrice/Amount"):
    print(price.text)

打印输出

16.69