2008-10-07 9 views
6

que estoy tratando de seleccionar un elemento HTML en un documento específico, para Firefox sólo tiene que utilizar:diferentes resultados seleccionando los elementos HTML con XPath en Firefox e Internet Explorer

xpathobj = document.evaluate(xpath, document, null, 
       XPathResult.FIRST_ORDERED_NODE_TYPE, null); 

que funciona muy bien. Sin embargo cuando intento equivilent la IE:

xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); 
xmlDoc.async=false; 
xmlDoc.load(document); 
xmlDoc.setProperty("SelectionLanguage", "XPath"); 
xpathobj = xmlDoc.selectNodes(xpath); 

consigo ningún objeto devuelto. ¿Entonces mi pregunta es una forma fácil de usar XPath para llegar al elemento que quiero en IE? El XPath que estoy usando se parece

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

Respuesta

0

¿Seguro XPath se implementa en su versión de Internet Explorer? Como en: ¿qué versión estás usando?

3

Eche un vistazo al proyecto http://dev.abiss.gr/sarissa/. Han migrado la mayoría de las API relacionadas con XML a IE. De lo contrario, también es fácil de implementar. Los problemas que necesitaría resolver serían: la serialización de HTML en XML válido, el resultado de la sincronización de la consulta XPath XMLDOM con HTMLDOM original. Que yo sepa, lo han hecho en su biblioteca, sin embargo, su rendimiento podría haber sido mejor.

0

en lugar de hacer

xmlDoc.load(document); 

tratan

xmlDoc.loadXML(document.body.outerHTML) 

Esto sólo sería realmente trabajar si su documento HTML es el formato para los estándares XHTML . Además, la etiqueta BODY sería el nodo raíz, por lo que tendría que cambiar su XPath para "/ cuerpo/DIV [9]/DIV [2]"

0

me gustaría ser un poco preocupado por el uso de XML como esto , ya que no puede estar seguro de qué versión (si la hay) de la DLL XML que tiene una persona. Todavía hay empresas que usan IE5.0 por ahí en tropel, y 5.5 tuvo una implementación de XML especialmente sobria.

1

Hola, al final se me ocurrió mi propia solución poco fiable, cualquier sugerencia sobre la mejora sería muy apreciada. Se hace uso de algunas funciones prototipo:

Obras en IE5 + con XPath de la forma "/ HTML/BODY/DIV [9]/DIV [2]"

función getXPathElement (XPath, elemento) {

//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++; 
} 

}

función getXPathElementByIndex (decendientes, xpathSegment) {

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; 

}

Gracias a todos por su ayuda, de cualquier forma aprendí bastante sobre varios frameworks de JavaScript.

3

El problema puede ser que en IE5 + [1] es de hecho [2] en FF. Microsoft decidió únicamente que la numeración debería comenzar en [0] y no [1] según lo especificado por w3c.

0

no puedo encontrar una solución simple y común, puede escribir la función personalizada para poner en práctica un poco de XPath, pero es difícil de conseguir completa en Internet Explorer 6 o una versión más baja ....

1

Hay algunos errores en el código de oly1234 Intento solucionarlo de la siguiente manera:

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; 
} 
Cuestiones relacionadas