Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- xpathobj = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
- xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
- xmlDoc.async=false;
- xmlDoc.load(document);
- xmlDoc.setProperty("SelectionLanguage", "XPath");
- xpathobj = xmlDoc.selectNodes(xpath);
- //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;
- }
- xmlDoc.load(document);
- xmlDoc.loadXML(document.body.outerHTML)
Add Comment
Please, Sign In to add comment