对XPath Expression进行故障排除以根据子节点选择节点

时间:2014-01-21 03:58:26

标签: html xpath

注意 - 这个问题与我提出的另一个问题非常相似 - Xpath Expression to select nodes based on presence of child node? - 然而,我正试图扩展它,并且失败了。

我有一个列出产品的HTML页面。

我正在尝试使用Xpath来区分可用产品和售罄产品。

可用产品如下所示:

<div class="product-widget-container">
    <article itemscope="" itemtype="http://schema.org/Product" class="product grid_4 full space omega large " data-productid="1996364" data-name="Daily Wrinkle Defence Essential Skin Reviver Cream Cleanser - 100ml" data-actual-price="5.99" data-is-available="true" data-low-stock="" data-popularity="6" data-smallimgsrc="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996364_94d4a520-7e4a-11e3-930f-000c29c9a057_image_310x434.JPG" data-largeimgsrc="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996364_94d4a520-7e4a-11e3-930f-000c29c9a057_image_310x434.JPG" data-sizes="[]" data-available-sizes="[]" data-categories="[119977]" data-brand="That Natural Source" data-discount="83" data-default-order="9">
        <figure>
            <div class="product-img-container ">
            <img itemprop="image" class="lazy product-img" src="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996364_94d4a520-7e4a-11e3-930f-000c29c9a057_image_310x434.JPG" data-original="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996364_94d4a520-7e4a-11e3-930f-000c29c9a057_image_310x434.JPG" alt="Up to 85% off Summer Looks Daily Wrinkle Defence Essential Skin Reviver Cream Cleanser - 100ml " style="display: inline;">
            <span class="arrow arrow-up"></span>
            <div class="quick-buy" style="display: none;">
            <span class="arrow-down-trans"></span>
            <div class="select-size">
            <form class="express-buy" action="/basket/add/1996364/" method="post">
            <input type="hidden" id="id_quantity_1996364" class="purchase-quantity" name="quantity" value="1">
            <input type="hidden" value="" name="addbasket.x">
            <span>
            <input class="add-to-basket btn btn-primary  btn-large " type="submit" value="ADD TO BASKET">
            </span>
            </form>
            </div>
            </div>
            </div>
            <a itemprop="url" class="overlay-link" href="/event/outlet/up-to-off-summer-looks/1996364-daily-wrinkle-defence-essential-skin-reviver-cream-cleanser-100ml/" title="Daily Wrinkle Defence Essential Skin Reviver Cream Cleanser - 100ml"></a>
            <figcaption>
            <h2 itemprop="name" class="mason name">
            That Natural Source: Daily Wrinkle Defence Essential Skin Reviver Cream Cleanser - 100ml
            </h2>
            <small itemprop="brand" class="bed"> Up to 85% off Summer Looks</small>
            <small class="bed shoes-price">
            $5.99
            <del>$34.95 RRP</del>
            <span class="discount">(83% discount)</span>
            </small>
            </figcaption>
        </figure>
    </article>
</div>

售罄产品如下所示:

<div class="product-widget-container">
    <article itemscope="" itemtype="http://schema.org/Product" class="product grid_4 full space omega large " data-productid="1996526" data-name="#T58 When Monkeys Fly! - Oz The Great And Powerful Collection By OPI" data-actual-price="10.99" data-is-available="" data-low-stock="true" data-popularity="1" data-smallimgsrc="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996526_d0402efe-7e4a-11e3-930f-000c29c9a057_image_310x434.jpg" data-largeimgsrc="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996526_d0402efe-7e4a-11e3-930f-000c29c9a057_image_310x434.jpg" data-sizes="[]" data-available-sizes="[]" data-categories="[119968]" data-brand="OPI" data-discount="0" data-default-order="39">
        <div class="stock-status be_sprites sold-out">Sold Out</div>
        <figure>
            <div class="product-img-container ">
            <img itemprop="image" class="lazy product-img" src="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996526_d0402efe-7e4a-11e3-930f-000c29c9a057_image_310x434.jpg" data-original="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996526_d0402efe-7e4a-11e3-930f-000c29c9a057_image_310x434.jpg" alt="Up to 85% off Summer Looks #T58 When Monkeys Fly! - Oz The Great And Powerful Collection By OPI " style="display: inline;">
            <span class="arrow arrow-up"></span>
            </div>
            <a itemprop="url" class="overlay-link" href="/event/outlet/up-to-off-summer-looks/1996526-t58-when-monkeys-fly-oz-the-great-and-powerful-collection-by-opi/" title="#T58 When Monkeys Fly! - Oz The Great And Powerful Collection By OPI"></a>
            <figcaption>
            <h2 itemprop="name" class="mason name">
            Opi: #T58 When Monkeys Fly! - Oz The Great And Powerful Collection By OPI
            </h2>
            <small itemprop="brand" class="bed"> Up to 85% off Summer Looks</small>
            <small class="bed shoes-price">
            $10.99
            </small>
            </figcaption>
        </figure>
    </article>
</div>  

我原以为我可以继续使用“售罄”课程或其中的Sold Out文字。

我已经尝试了以下所有方法,但似乎没有一个工作 - 它们都给了我全套产品:

//div[@class="product-widget-container" and not(div[@class="stock-status be_sprites sold-out"])]

//div[@class="product-widget-container" and not(div[contains(@class, "sold-out")])]

//div[@class="product-widget-container" and not(div[contains(., "Sold Out")])]

对我在XPath表达式中做错了什么的想法?

干杯, 维克多

2 个答案:

答案 0 :(得分:0)

//div[@class='product-widget-container' and not(@class='stock-status be_sprites sold-out')]

您应该删除谓词

中的div[]

答案 1 :(得分:0)

您的表达方式有正确的想法,但您不需要嵌套[ ]括号。一旦打开它们,您就处于条件声明中:您编写的所有内容都将成为声明的一部分。因此,当您想要检查子节点的属性时,您只需选择它:node[child/@attribute]

您还需要检查任何深度的div,因为它不是第一个子节点。如果您写div[div/@class="foo"],则表示您正在检查<div><div class="foo"></div></div>。如果您撰写div[.//div/@class="foo"],则表示您正在检查<div><anything><bar><div class="foo"></div></bar></anything></div>

这样的东西
//div[@class="product-widget-container" and not(.//div/@class="stock-status be_sprites sold-out")]

应该有用!