code_junkie

Problem selecting HTML elements with xpath in Internet Explorer

Nov 11th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. xpathobj = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  2.  
  3. xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  4. xmlDoc.async=false;
  5. xmlDoc.load(document);
  6. xmlDoc.setProperty("SelectionLanguage", "XPath");
  7. xpathobj = xmlDoc.selectNodes(xpath);
  8.  
  9. //Specific to project, here i know that the body element will always have the id "top"
  10. //but otherwise the element that is passed in should be first element in the xpath
  11. //statement eg. /HTML/BODY/DIV the element passed in should be HTML
  12. if(!element){
  13. element = $("top");
  14. var xpathArrayIndex = 3;
  15. } else {
  16. var xpathArrayIndex = 1;
  17. }
  18. //split the xpath statement up
  19. var xpathArray = xpath.split("/");
  20.  
  21. var carryOn = true;
  22. while(carryOn){
  23. decendents = element.childElements();
  24. //check to see if we are at the end of the xpath statement
  25. if(xpathArrayIndex == xpathArray.length){
  26. return element;
  27. }
  28. //if there is only one decendent make it the next element
  29. if(decendents.size() == 1) {
  30. element = decendents.first();
  31. } else {
  32. //otherwise search the decendents for the next element
  33. element = getXPathElementByIndex(decendents, xpathArray[xpathArrayIndex]);
  34. }
  35. xpathArrayIndex++;
  36. }
  37.  
  38. var decendentsArray = decendents.toArray();
  39. //seperate the index from the element name
  40. var temp = xpathSegment.split("[");
  41. var elementName = temp[0];
  42. //get the index as a number eg. "9]" to 9
  43. var elementIndex = +temp[1].replace("]", "");
  44. //the number of matching elements
  45. var count = 0;
  46.  
  47. //keeps track of the number of iterations
  48. var i = 0;
  49. while(count != elementIndex) {
  50. //if the decendent's name matches the xpath element name increment the count
  51. if(decendentsArray[i].nodeName == elementName){
  52. count++;
  53. }
  54. i++;
  55. }
  56. var element = decendentsArray[i - 1];
  57. return element;
  58.  
  59. function getXPathElement(xpath, element){
  60. if(!element){
  61. element = document;
  62. }
  63. var xpathArray = xpath.split("/");
  64.  
  65. element = findXPathRoot(xpathArray[0],xpathArray[1],element);
  66.  
  67. for(var i=1; i<xpathArray.length; i++){
  68. if(xpathArray[i].toLowerCase()=="html"){
  69. continue;
  70. }
  71. if(!element){
  72. return element;
  73. }
  74. element = getXPathElementByIndex(element.childNodes,xpathArray[i]);
  75. }
  76. return element;
  77. }
  78.  
  79.  
  80. function findXPathRoot(rootPath,htmlPath,element){
  81. if(rootPath == ""&&htmlPath.toLowerCase() == "html"){
  82. return element.documentElement;
  83. }
  84. return document.getElementsByTagName(rootPath)[0];
  85. }
  86. function getXPathElementByIndex(decendents, xpathSegment){
  87. //seperate the index from the element name
  88. var temp = xpathSegment.split("[");
  89. var elementName = temp[0];
  90. //get the index as a number eg. "9]" to 9
  91. if(temp[1]){
  92. var elementIndex = temp[1].replace("]", "");
  93. }else{
  94. var elementIndex = 1;
  95. }
  96. //the number of matching elements
  97. var count = 0;
  98. for(var i=0;i < decendents.length; i++){
  99. if (decendents[i].nodeName.toLowerCase() == elementName.toLowerCase()) {
  100. count++;
  101. if(count==elementIndex){
  102. return decendents[i];
  103. }
  104. }
  105. }
  106. return null;
  107. }
  108.  
  109. xmlDoc.load(document);
  110.  
  111. xmlDoc.loadXML(document.body.outerHTML)
Add Comment
Please, Sign In to add comment