我正在使用PHP檢索給定URL和XPATH的內容。 我使用DOMDocument/DOMXPath(與查詢或評估)。DOMXpath查詢/評估的xpath過長太長
對於小的xpath,我獲得正確的結果,但對於更長的xpath,它不起作用。 (這XPath的似乎也不錯(我Xpather(火狐插件)獲得他們和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不起作用。
給我們xml和xpath – 2010-11-04 13:44:10
我添加一個例子。 – AlphaB 2010-11-04 14:04:23