如何让这个javascript函数在xpath中输出@value?

时间:2009-10-06 03:22:10

标签: javascript xpath

下面的代码生成Xpath。但是,它不显示@value属性/属性。它工作得不好。

function getXPath(node, path, val) {
path = path || [];
if(node.parentNode) { path = getXPath(node.parentNode, path); }

if(node.previousSibling) {
  var count = 1;
  var sibling = node.previousSibling
  do {
    if(sibling.nodeType == 1 && sibling.nodeName == node.nodeName) {count++;}
    sibling = sibling.previousSibling;
  } while(sibling);
  if(count == 1) {count = null;}
  } else if(node.nextSibling) {
    var sibling = node.nextSibling;
  do {
    if(sibling.nodeType == 1 && sibling.nodeName == node.nodeName) {
      var count = 1;
      sibling = null;
    } else {
      var count = null;
      sibling = sibling.previousSibling;
    }
  } while(sibling);
}

if(node.nodeType == 1) {
  if (val){
  path.push(node.nodeName.toLowerCase() + (node.id ? 
           "[@id='"+node.id+"' @value='"+val+"']" : 
               count > 0 ? "["+count+"]" : ''));
  }else{
    path.push(node.nodeName.toLowerCase() + (node.id ? 
           "[@id='"+node.id+"']" : count > 0 ? "["+count+"]" : ''));
  }
}
    return path;
};

1 个答案:

答案 0 :(得分:0)