MozTW 討論區
https://forum.moztw.org/

xpath is not working in firefox 3.6 (xpath 程式不work)
https://forum.moztw.org/viewtopic.php?f=2&t=29430
1 頁 (共 1 頁)

發表人:  chinghunglai [ 2010-02-03, 09:55 ]
文章主題 :  xpath is not working in firefox 3.6 (xpath 程式不work)

原本使用 xpath 的程式在 firefox 3.0-3.5 使用正常
但是在 3.6 無法正常運作

以下是我把無法使用的程式獨立成一個簡短的 html 檔案,以便觀看
代碼:
<script>
        window.onload = function(){
                var aDoc = document.implementation.createDocument("", "", null);
                var aElem = document.createElement('DIV');
                aElem.innerHTML = "<table id='ptable'><tr><td>this is a table</td></tr></table>";
                aDoc.appendChild(aElem);
                var ret = aDoc.evaluate("//table[@id='ptable']", aElem, null,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
                alert("navigator.userAgent : " + navigator.userAgent + "\r\n\r\n" + "ret.singleNodeValue : " + ret.singleNodeValue);
        }
</script>

我也將這個 html 檔放上網路,以便測試
http://chin99.myweb.hinet.net/code/test_xpath.html

請問有人遇到類似的問題嗎?或是有已知的解決方案?感謝!

發表人:  gandalf_Zoro [ 2010-02-03, 20:19 ]
文章主題 :  Re: xpath is not working in firefox 3.6 (xpath 程式不work)

加一行就可以看到 3.5 跟 3.6 分別
代碼:
 alert(aElem.innerHTML);


3.6 多了 xmlns="http://www.w3.org/1999/xhtml"

好像是 namespace 問題

https://developer.mozilla.org/en/Introd ... JavaScript

還是要等專業的來

發表人:  angusdev [ 2010-02-04, 22:22 ]
文章主題 :  Re: xpath is not working in firefox 3.6 (xpath 程式不work)

用 Namespace resolver, 在 xpath express 裡的每個 tag 加上一個 namespace,
再在 resolver 根據 Firefox 的 version 來傳回適當的 URI

代碼:
var nsResolver = {
  lookupNamespaceURI:function (prefix) {
    if (isFF36up && prefix == "ns") {
      return "http://www.w3.org/1999/xhtml";
    }
    else {
      return "";
    }
  }
};

var ret = aDoc.evaluate("//ns:table[@id='ptable']", aElem, nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);

1 頁 (共 1 頁) 所有顯示的時間為 UTC + 8 小時
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/