Search code examples
phpdomxpathdomdocument

How to Exclude Class in Parent Xpath Class? PHP


Driving me up the wall, it's like this, the DOM

<div class="product-intro"><p class="product-desc"><span class="product-model">234</span>Product Description</p></div>

It's not anything like this... invalid argument, no nodes found:

$node3 = $xp->query("//p[@class='product-desc and not(@class='product-model']");
$node3 = $xp->query("//p[@class='product-desc'][not([@class='product-model'])]");

This alone:

$node3 = $xp->query("//p[@class='product-desc']");

Works perfectly well and fine-- as far as getting a result. The output is

234Product Description

I know I could just do a string replace, but not ideally.. How do I get it to exclude the product-model class in my query?

Entire script:

$x = '<div class="product-item productContainer" data-itemno="234">
    <div class="product-and-intro">
        <div class="product">
            <a href="/en/234.html" title="Product Description"> 
                <img src="/ProductImages/106/234.jpg" alt="Product Description" class="itemImage" />
                <div class="product-intro">
                    <p class="product-desc"><span class="product-model">234</span>Product Description</p>
                    <p class="price"><span class="us">US$</span>6.50 <span class="oldprice"><s>$ 13.00</s></span></p>
                </div>
            </a>
        </div>
    </div>
</div>';



$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($x);
$xp = new DOMXPath($dom);
$node1 = $xp->query("//div[@class='product']//img");
$node2 = $xp->query("//span[@class='product-model']");
$node3 = $xp->query("//p[@class='product-desc']");
// $node3 = $xp->query("//p[@class='product-desc']/text()[2]");
// $node3 = $xp->query("//p[@class='product-desc' and not(span/@class='product-model')]");
$node4 = $xp->query("//p[@class='price']");

foreach ($node1 as $n) {
    echo $n->getAttribute('src');
    echo '<br>';
}

foreach ($node2 as $n2) {
    echo $n2->nodeValue;
        echo '<br>';
}

foreach ($node3 as $n3) {
    echo $n3->nodeValue;
        echo '<br>';
}

foreach ($node4 as $n4) {
    echo $n4->nodeValue;
        echo '<br>';
}

Solution

  • If you want to select all p elements with a @class attribute value of product-desc and then filter out those who have a span sub-element with the @class attribute value product-model you can use this XPath expression:

    //p[@class='product-desc' and not(span/@class='product-model')]
    

    Or, in a whole

    $node3 = $xp->query("//p[@class='product-desc' and not(span/@class='product-model')]");