PHPを使用して、特定のURLとXPATHのコンテンツを取得しています。私はDOMDocument/DOMXPathを使用しています(クエリまたは評価を使用)。
小さいxpathの場合、正しい結果が得られますが、長いxpathの場合、機能しません。(そして、このxpathは良いようです(私はXpather(firefoxプラグイン)でそれらを取得し、YQLでそれらを再テストしました)。
この不思議なトラブルについて何かアドバイスはありますか?
コードの例:
$doc = new DOMDocument();
$myXMLString = file_get_contents('http://stackoverflow.com/questions/4097230/too-long-xpath-with-domxpath-query-evaluate-return-nothing');
@$doc->loadHTML($myXMLString); //@ to suppress warnings
//(good for not ending markup)
$xpath = new DOMXPath($doc);
$fullPath ="/html/body/small/path"; //it works
//$fullPath = "/html/body/full/path/with/lot/of/markup";//does not works
$entries = $xpath->query($fullPath);
//or ->evalutate($fullPath) (same behaviour)
//$entries return DOMNodeList (empty for a long path query,
// correct for a small path query)
属性制限を使用してテストしましたが、変更されていないようです(xpathが小さい場合は機能し、長い場合は機能しません)
例:この現在のページの場合:
$fullPath = "/html
/body
/div[4]
/div[@id='content']
/div[@id='question-header']
/h1
/a";//works (retrieve the question title)
$fullPath = "/html
/body
/div[4]
/div[@id='content']
/div[@id='mainbar']
/div[@id='question']
/table
/tbody
/tr[2]
/td[2]
/div[@id='comments-4097230']
/table
/tbody
/tr[@id='comment-4408626']
/td[2]
/div
/a"; //does'nt work
//(should retrieve 'gaby' from comment)
編集:
SimpleXML libでテストしましたが、まったく同じ動作をします(小さなクエリでは良い結果が得られ、長いクエリでは何も起こりません)。
編集2:
また、最初の要素を削除して最長のxpathを切り取り、機能します。ところで、私は完全に正しいxpathが機能しない理由を本当に理解していません。