【问题标题】:Troubleshooting XPath Expression to select nodes based on child node基于子节点选择节点的 XPath 表达式疑难解答
【发布时间】:2014-01-21 16:34:25
【问题描述】:

注意 - 这个问题与我提出的另一个问题非常相似 - 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 表达式中做错了什么有什么想法吗?

干杯, 维克多

【问题讨论】:

    标签: html xpath


    【解决方案1】:

    您的表达式有正确的想法,但您不需要嵌套[ ] 括号。一旦你打开它们,你就处于一个条件语句中:你写的所有东西都将成为语句的一部分。所以当你要查看某个子节点的属性时,只需要选中它:node[child/@attribute]

    您还需要检查任意深度的div,因为它不是第一个子节点。如果你写div[div/@class="foo"],你正在检查&lt;div&gt;&lt;div class="foo"&gt;&lt;/div&gt;&lt;/div&gt;。如果你写div[.//div/@class="foo"],你正在检查&lt;div&gt;&lt;anything&gt;&lt;bar&gt;&lt;div class="foo"&gt;&lt;/div&gt;&lt;/bar&gt;&lt;/anything&gt;&lt;/div&gt;

    类似

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

    应该工作!

    【讨论】:

      【解决方案2】:

      试试

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

      您应该删除谓词中的div[]

      【讨论】:

        猜你喜欢
        • 2012-02-18
        • 1970-01-01
        • 2016-10-03
        • 2013-03-03
        • 1970-01-01
        • 2017-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多