Simple XPath predicate not working

放肆的年华 提交于 2020-01-16 02:33:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!