var xpath = "/source/title[1]"
解决方案 »
- jquery jsTree 外部如何触发 jsTree callBack下的onChang方法
- 再现Google《芝麻街》Doodle的效果
- 为什么这段切换图片的代码在IE下能起作用,而在Firefox下却不起作用???
- 求教一个页面跳转问题
- innerHTML与form表单
- 如何使用js在当前页面中生成一段CSS文本
- 怎么样生成一个随机的颜色值?
- 如何在右边框架中刷新左侧框架中的页面(在线,搞定立刻结贴)
- 3721把我的浮动菜单给拦截了,怎么解决????(-----高手赐教,向3721宣战!!!!!!!!)
- 求一正则表达式
- CSDN社区,左边的这个树型是什么实现的?求助,在线等
- JS删除表格的行 怎样删除指定ID的某<TR></TR>?
0就没有意义了可以理解为下标从1开始
从1开始?确定吗?
上面的那些代码主要是为了在ie模拟xml的selectNode,selectSingleNode,在ie中是从0开始的,要如果是这样岂不是就很难统一了。
所有代码如下
// prototying the XMLDocument
XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
{
if( !xNode ) { xNode = this; }
var oNSResolver = this.createNSResolver(this.documentElement);
var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var aResult = [];
for( var i = 0; i < aItems.snapshotLength; i++)
{
aResult[i] = aItems.snapshotItem(i);
}
return aResult;
}// prototying the Element
Element.prototype.selectNodes = function(cXPathString)
{
if(this.ownerDocument.selectNodes)
{
return this.ownerDocument.selectNodes(cXPathString, this);
}
else{throw "For XML Elements Only";}
}
}// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
// prototying the XMLDocument
XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
{
if( !xNode ) { xNode = this; }
var xItems = this.selectNodes(cXPathString, xNode);
if( xItems.length > 0 )
{
return xItems[0];
}
else
{
return null;
}
}// prototying the Element
Element.prototype.selectSingleNode = function(cXPathString)
{
if(this.ownerDocument.selectSingleNode)
{
return this.ownerDocument.selectSingleNode(cXPathString, this);
}
else{throw "For XML Elements Only";}
}
}
{
Node.prototype.selectNodes = function(sExpr)
{
var doc = this.nodeType==9 ? this : this.ownerDocument;
var nsRes = doc.createNSResolver(this.nodeType==9 ? this.documentElement : this);
var nsRes2; if (doc._selectionNamespaces)
{
nsRes2 = function(s)
{
if (s in doc._selectionNamespaces)
{
return doc._selectionNamespaces[s];
} return nsRes.lookupNamespaceURI(s);
};
}
else
{
nsRes2=nsRes;
} var xpRes = doc.evaluate(sExpr, this, nsRes2, 5, null);
var res=[];
var item; while ((item = xpRes.iterateNext()))
res.push(item); return res;
};Node.prototype.selectSingleNode = function(sExpr)
{
var doc = this.nodeType==9 ? this : this.ownerDocument;
var nsRes = doc.createNSResolver(this.nodeType==9 ? this.documentElement : this);
var nsRes2; if(doc._selectionNamespaces)
{
nsRes2 = function(s)
{
if(s in doc._selectionNamespaces)
{
return doc._selectionNamespaces[s];
} return nsRes.lookupNamespaceURI(s);
};
}
else
{
nsRes2=nsRes;
} var xpRes = doc.evaluate(sExpr, this, nsRes2, 9, null);
return xpRes.singleNodeValue;
};
}
(
"outerHTML",
function()
{
var str = "<" + this.tagName; for (var i = 0; i < this.attributes.length; i++)
{
var attr = this.attributes[i]; str += " ";
str += attr.nodeName + "=" + '"' + attr.nodeValue + '"';
} str += ">" + this.innerHTML + "</" + this.tagName + ">"; return str;
}
);HTMLElement.prototype.__defineGetter__
(
"innerText",
function()
{
var rng = document.createRange();
rng.selectNode(this); return rng.toString();
}
);HTMLElement.prototype.__defineSetter__
(
"innerText",
function(txt)
{
var rng = document.createRange();
rng.selectNodeContents(this);
rng.deleteContents();
var newText = document.createTextNode(txt);
this.appendChild(newText);
}
);
/* event.srcElement || event.target */Event.prototype.__defineGetter__
(
"srcElement",
function()
{
return this.target;
}
);/* Object.attachEvent || Object.addEventListener
Object.detachEvent || Object.removeEventListener */HTMLElement.prototype.attachEvent = Node.prototype.attachEvent = function(a, b)
{
return this.addEventListener(a.replace(/^on/i, ""), b,false);
};HTMLElement.prototype.detachEvent = Node.prototype.detachEvent = function(a, b)
{
return this.removeEventListener(a.replace(/^on/i, ""), b, false);
};/* XMLDocument.onreadystatechange, parseError
XMLDocument.createNode, load, loadXML,setProperty */var _xmlDocPrototype = XMLDocument.prototype;XMLDocument.prototype.__defineSetter__
(
"onreadystatechange",
function(f)
{
if (this._onreadystatechange)
{
this.removeEventListener("load", this._onreadystatechange, false);
}
this._onreadystatechange = f; if (f)
{
this.addEventListener("load", f, false);
} return f;
}
);XMLDocument.prototype.createNode = function(aType, aName, aNamespace)
{
switch(aType)
{
case 1:
if (aNamespace && aNamespace != "")
{
return this.createElementNS(aNamespace, aName);
}
else
{
return this.createElement(aName);
}
case 2:
if (aNamespace && aNamespace != "")
{
return this.createAttributeNS(aNamespace,aName);
}
else
{
return this.createAttribute(aName);
}
case 3:
default:
return this.createTextNode("");
}
};XMLDocument.prototype.__realLoad = _xmlDocPrototype.load;XMLDocument.prototype.load = function(sUri)
{
this.readyState = 0;
this.__realLoad(sUri);
};XMLDocument.prototype.loadXML = function(s)
{
var doc2 = (new DOMParser).parseFromString(s, "text/xml"); while (this.hasChildNodes())
{
this.removeChild(this.lastChild);
}
var cs=doc2.childNodes;
var l = cs.length; for (var i = 0; i < l; i++)
{
this.appendChild(this.importNode(cs[i], true));
}
};XMLDocument.prototype.setProperty = function(sName, sValue)
{
if (sName == "SelectionNamespaces")
{
this._selectionNamespaces = {}; var parts = sValue.split(/\s+/);
var re= /^xmlns\:([^=]+)\=((\"([^\"]*)\")|(\'([^\']*)\'))$/; for (var i = 0; i < parts.length; i++)
{
re.test(parts[i]);
this._selectionNamespaces[RegExp.$1] = RegExp.$4 || RegExp.$6;
}
}
};var _mozHasParseError = function(oDoc)
{
return !oDoc.documentElement || oDoc.documentElement.localName=="parsererror"
&& oDoc.documentElement.getAttribute("xmlns")=="http://www.mozilla.org/newlayout/xml/parsererror.xml";
};XMLDocument.prototype.__defineGetter__
(
"parseError",
function()
{
var hasError = _mozHasParseError(this); var res = {
errorCode : 0,
filepos : 0,
line : 0,
linepos : 0,
reason : "",
srcText : "",
url : ""
}; if (hasError)
{
res.errorCode = -1;
try
{
res.srcText = this.getElementsByTagName("sourcetext")[0].firstChild.data;
res.srcText = res.srcText.replace(/\n\-\^$/, "");
}
catch(ex)
{
res.srcText = "";
} try
{
var s = this.documentElement.firstChild.data;
var re = /XML Parsing Error\:(.+)\nLocation\:(.+)\nLine Number(\d+)\,Column(\d+)/;
var a = re.exec(s);
res.reason = a[1];
res.url = a[2];
res.line = a[3];
res.linepos = a[4];
}
catch(ex)
{
res.reason="Unknown";
}
} return res;
}
);
/* Attr.xml */Attr.prototype.__defineGetter__
(
"xml",
function()
{
var nv = (new XMLSerializer).serializeToString(this); return this.nodeName + "=\""+nv.replace(/\"/g,""")+"\"";
}
);
/* Node.xml, text, baseName
Node.selectNodes, selectSingleNode, transformNode, transformNodeToObject */Node.prototype.__defineGetter__
(
"xml",
function()
{
return (new XMLSerializer).serializeToString(this);
}
);Node.prototype.__defineGetter__
(
"text",
function()
{
var cs = this.childNodes;
var l = cs.length;
var sb = new Array(l); for (var i = 0; i < l; i++)
{
sb[i] = cs[i].text.replace(/^\n/, "");
} return sb.join("");
}
);Node.prototype.__defineGetter__
(
"baseName",
function()
{
var lParts = this.nodeName.split(":"); return lParts[lParts.length-1];
}
);
Node.prototype.selectNodes = function(sExpr)
{
var doc = this.nodeType==9 ? this : this.ownerDocument;
var nsRes = doc.createNSResolver(this.nodeType==9 ? this.documentElement : this);
var nsRes2; if (doc._selectionNamespaces)
{
nsRes2 = function(s)
{
if (s in doc._selectionNamespaces)
{
return doc._selectionNamespaces[s];
} return nsRes.lookupNamespaceURI(s);
};
}
else
{
nsRes2=nsRes;
} var xpRes = doc.evaluate(sExpr, this, nsRes2, 5, null);
var res=[];
var item; while ((item = xpRes.iterateNext()))
res.push(item); return res;
};
Node.prototype.selectSingleNode = function(sExpr)
{
var doc = this.nodeType==9 ? this : this.ownerDocument;
var nsRes = doc.createNSResolver(this.nodeType==9 ? this.documentElement : this);
var nsRes2; if(doc._selectionNamespaces)
{
nsRes2 = function(s)
{
if(s in doc._selectionNamespaces)
{
return doc._selectionNamespaces[s];
} return nsRes.lookupNamespaceURI(s);
};
}
else
{
nsRes2=nsRes;
} var xpRes = doc.evaluate(sExpr, this, nsRes2, 9, null);
return xpRes.singleNodeValue;
};Node.prototype.transformNode = function(oXsltNode)
{
var doc = this.nodeType==9 ? this : this.ownerDocument;
var processor = new XSLTProcessor();
processor.importStylesheet(oXsltNode);
var df = processor.transformToFragment(this, doc); return df.xml;
};Node.prototype.transformNodeToObject = function(oXsltNode, oOutputDocument)
{
var doc = this.nodeType==9 ? this : this.ownerDocument;
var outDoc = oOutputDocument.nodeType==9 ? oOutputDocument : oOutputDocument.ownerDocument;
var processor=new XSLTProcessor();
processor.importStylesheet(oXsltNode);
var df=processor.transformToFragment(this,doc); while (oOutputDocument.hasChildNodes())
{
oOutputDocument.removeChild(oOutputDocument.lastChild);
} var cs=df.childNodes;
var l=cs.length;
for(var i=0; i<l; i++)
{
oOutputDocument.appendChild(outDoc.importNode(cs[i],true));
}
};
/* TextNode.text */Text.prototype.__defineGetter__
(
"text",
function()
{
return this.nodeValue;
}
);
//其它一些mozilla没有的属性和方法.
谢谢,用你方法,执行下面的语句:node为null,而在ie下,node不为null,是不是在firefox下,类似"/source/title[0]"这样的xpath,下标是从1开始的?var xmlStr = "<source><title att='att1'/><author>John Smith</author></source>";
var doc = document.implementation.createDocument("text/xml", "", null);
doc.loadXML(xmlStr);
var nodes = doc.selectNodes("/source/title");
var node = doc.selectSingleNode("/source/title[0]");
alert(node);
这样解决.