0
我使用此鏈接提供的自定義JavaScript函數(http://km0.la/js/mozXPath/)在FireFox中實現特定的XML功能。爲XML原型函數定義JavaScript的原型屬性
下面是代碼:
// mozXPath
// Code licensed under Creative Commons Attribution-ShareAlike License
// http://creativecommons.org/licenses/by-sa/2.5/
if(document.implementation.hasFeature("XPath", "3.0")) {
if(typeof XMLDocument == "undefined") { XMLDocument = Document; }
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;
}
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; }
}
Element.prototype.selectNodes = function(cXPathString) {
if(this.ownerDocument.selectNodes) {
return this.ownerDocument.selectNodes(cXPathString, this);
}
else { throw "For XML Elements Only"; }
}
Element.prototype.selectSingleNode = function(cXPathString) {
if(this.ownerDocument.selectSingleNode) {
return this.ownerDocument.selectSingleNode(cXPathString, this);
}
else { throw "For XML Elements Only"; }
}
}
假設已經定義並加載XML內容的XML對象,這裏是一個如何訪問一個名爲XML標籤「cd_rank」的例子:
var cd_rank_XMLObj = XMLObj.selectSingleNode("cd_rank");
我想要做的是將屬性「nodeTypedValue」添加到selectSingleNode()函數,但我不知道如何做到這一點。在Element.prototype.selectSingleNode功能,我嘗試添加:
this.prototype.nodeTypedValue = this.textContent;
但是,它給了我一個錯誤說這是不確定的。我甚至嘗試添加它的功能之外,只是啞下來,並得到了概念,它也說,它是不確定的:
var XMLObj.selectSingleNode.prototype.nodeTypedValue = XMLObj.textContent;
alert(XMLObj.selectSingleNode("cd_rank").nodeTypedValue);
基本上我想要做的,我想,就是添加一個原型屬性轉換爲原型功能。但我需要一些幫助。我如何添加「nodeTypedValue」,使我寫「XMLObj.selectSingleNode(Path).nodeTypedValue」?