Javascript 在Firefox和Internet Explorer中使用XPath选择HTML元素的不同结果

Javascript 在Firefox和Internet Explorer中使用XPath选择HTML元素的不同结果,javascript,internet-explorer,xpath,Javascript,Internet Explorer,Xpath,我试图在文档中选择一个特定的HTML元素,对于firefox,我只使用: xpathobj = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); 这很好用。但是,当我尝试IE等效时: xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.load(docu

我试图在文档中选择一个特定的HTML元素,对于firefox,我只使用:

xpathobj = document.evaluate(xpath, document, null,
               XPathResult.FIRST_ORDERED_NODE_TYPE, null);
这很好用。但是,当我尝试IE等效时:

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(document);
xmlDoc.setProperty("SelectionLanguage", "XPath");
xpathobj = xmlDoc.selectNodes(xpath);
我没有收到归还的物品。所以我的问题是,有没有一种简单的方法可以使用XPath访问IE中需要的元素? 我使用的XPath看起来像

/HTML/BODY/DIV[9]/DIV[2]

您确定您的Internet Explorer版本中实现了X-Path吗?例如:您使用的是什么版本?

看看project。他们已经将大多数与XML相关的API迁移到IE。 否则,它确实也很容易实现。您需要解决的问题是:将HTML序列化为有效的XML,将XMLDOM XPath查询的结果与原始HTMLDOM同步。据我所知,他们已经在他们的库中完成了,但是,它的性能本可以更好。

jQuery实现。您的示例“/HTML/BODY/DIV[9]/DIV[2]”应该适用于它

(编辑-由于Sergey Ilinsky而更正)

而不是执行

xmlDoc.load(document); 
试一试


只有将HTML文档格式化为XHTML标准时,这才真正起作用。此外,BODY标记将是根节点,因此您必须将XPATH更改为“/BODY/DIV[9]/DIV[2]”

我有点担心这样使用xml,因为您无法确定一个人拥有的xml DLL的版本(如果有的话)。现在仍有很多公司在大量使用IE5.0,而5.5的XML实现尤其糟糕。

嗨,最后我提出了自己的诡计解决方案,任何关于改进它的建议都将受到极大的赞赏。它利用了一些原型功能:

在IE5+中使用格式为“/HTML/BODY/DIV[9]/DIV[2]”的xpath

函数getXPathElement(xpath,元素){

}

函数getXPathElementByIndex(Decents,xpathSegment){

}


感谢大家的帮助,不管怎样,我都学到了很多关于各种javascript框架的知识

可以找到的另一个JavaScript实现。但似乎没有激活。

问题可能是IE5+[1]中的[2]实际上是FF中的[2]。微软完全决定编号应从[0]开始,而不是w3c规定的[1]开始。

我找不到一个简单而通用的解决方案,您可以编写自定义函数来实现一点xpath,但在internet explorer 6或更低版本中很难完成……

oly1234的代码中存在一些错误,我尝试按如下方式修复它:

function getXPathElement(xpath, element){
if(!element){
    element = document;
}
var xpathArray = xpath.split("/");

element = findXPathRoot(xpathArray[0],xpathArray[1],element);

for(var i=1; i<xpathArray.length; i++){
    if(xpathArray[i].toLowerCase()=="html"){
        continue;
    }
    if(!element){
        return element;
    }
    element = getXPathElementByIndex(element.childNodes,xpathArray[i]);         
}
return element;
}


function findXPathRoot(rootPath,htmlPath,element){
if(rootPath == ""&&htmlPath.toLowerCase() == "html"){
    return element.documentElement;
}
return document.getElementsByTagName(rootPath)[0];
}
function getXPathElementByIndex(decendents, xpathSegment){
//seperate the index from the element name
var temp = xpathSegment.split("[");
var elementName = temp[0];
//get the index as a number eg. "9]" to 9
if(temp[1]){
    var elementIndex = temp[1].replace("]", "");
}else{
    var elementIndex = 1;
}
//the number of matching elements
var count = 0;
for(var i=0;i < decendents.length; i++){
    if (decendents[i].nodeName.toLowerCase() == elementName.toLowerCase()) {
        count++;
        if(count==elementIndex){
            return decendents[i];
        }
    }
}
return null;
}
函数getXPathElement(xpath,元素){ if(!元素){ 元素=文件; } var xpathArray=xpath.split(“/”); 元素=findXPathRoot(xpathArray[0],xpathArray[1],元素);
for(var i=1;ijQuery不实现XPath。它实现了一个非常简单的路径选择功能(约为XPath的0.01%)
//Specific to project, here i know that the body element will always have the id "top"
//but otherwise the element that is passed in should be first element in the xpath 
//statement eg. /HTML/BODY/DIV the element passed in should be HTML
if(!element){
    element = $("top");
    var xpathArrayIndex = 3;
} else {
    var xpathArrayIndex = 1;
}
//split the xpath statement up
var xpathArray = xpath.split("/");

var carryOn = true; 
while(carryOn){
    decendents = element.childElements();
    //check to see if we are at the end of the xpath statement
    if(xpathArrayIndex == xpathArray.length){
        return element;
    }
    //if there is only one decendent make it the next element
    if(decendents.size() == 1) {
        element = decendents.first();
    } else {
    //otherwise search the decendents for the next element
        element = getXPathElementByIndex(decendents, xpathArray[xpathArrayIndex]);
    }
    xpathArrayIndex++;
}
var decendentsArray = decendents.toArray();
//seperate the index from the element name
var temp = xpathSegment.split("[");
var elementName = temp[0];
//get the index as a number eg. "9]" to 9
var elementIndex = +temp[1].replace("]", "");
//the number of matching elements
var count = 0;

//keeps track of the number of iterations
var i = 0;
while(count != elementIndex) {
    //if the decendent's name matches the xpath element name increment the count
    if(decendentsArray[i].nodeName == elementName){
        count++;
    }
    i++;
}
var element = decendentsArray[i - 1];
return element;
function getXPathElement(xpath, element){
if(!element){
    element = document;
}
var xpathArray = xpath.split("/");

element = findXPathRoot(xpathArray[0],xpathArray[1],element);

for(var i=1; i<xpathArray.length; i++){
    if(xpathArray[i].toLowerCase()=="html"){
        continue;
    }
    if(!element){
        return element;
    }
    element = getXPathElementByIndex(element.childNodes,xpathArray[i]);         
}
return element;
}


function findXPathRoot(rootPath,htmlPath,element){
if(rootPath == ""&&htmlPath.toLowerCase() == "html"){
    return element.documentElement;
}
return document.getElementsByTagName(rootPath)[0];
}
function getXPathElementByIndex(decendents, xpathSegment){
//seperate the index from the element name
var temp = xpathSegment.split("[");
var elementName = temp[0];
//get the index as a number eg. "9]" to 9
if(temp[1]){
    var elementIndex = temp[1].replace("]", "");
}else{
    var elementIndex = 1;
}
//the number of matching elements
var count = 0;
for(var i=0;i < decendents.length; i++){
    if (decendents[i].nodeName.toLowerCase() == elementName.toLowerCase()) {
        count++;
        if(count==elementIndex){
            return decendents[i];
        }
    }
}
return null;
}