问题
I'm running the following PHP snippet that evaluates an XPath query agains the source of a HTML page. The query seems correct and I tested it with some online XPath testers, but I won't get any match.
<?php
$details = new DOMDocument();
@$details->loadHTMLFile('http://www.astagiudiziaria.com/beni/lotto_unico_genova_via_della_pigna_6b_-_proc_n_583_14_trib_di_genova/index.html');
$xpath = new DOMXpath($details);
$procedimento = $xpath->query('.//ul[preceding-sibling::h2="Informazioni sulla procedura"]/li[(starts-with(., "R.G.E. N°") or starts-with(., "N°")) and not(starts-with(., "N° IVG"))]');
echo $procedimento->length; // returns 0, expected 1
?>
Seems that removing the part and not(starts-with(., "N° IVG")) solves the problem (but I need this clause).
What maybe the problem with that XPath query?
回答1:
I'd forget to check if the source contains spaces in the source. This works:
.//ul[preceding-sibling::h2="Informazioni sulla procedura"]/li[(starts-with(normalize-space(.), "R.G.E. N°") or starts-with(normalize-space(.), "N°")) and not(starts-with(normalize-space(.), "N° IVG: "))]
来源:https://stackoverflow.com/questions/34266482/simple-xpath-predicate-not-working