sarissa.js
来自「一个使用struts+hibernate+spring开发的完的网站源代码。」· JavaScript 代码 · 共 735 行 · 第 1/2 页
JS
735 行
* on XML documents with a default namespace, as there is no other way * of mapping that to a prefix.</p> * <p>Using no namespace prefix in DOM Level 3 XPath queries, implies you * are looking for elements in the null namespace. If you need to look * for nodes in the default namespace, you need to map a prefix to it * first like:</p> * <pre>Sarissa.setXpathNamespaces(oDoc, "xmlns:myprefix=&aposhttp://mynsURI&apos");</pre> * <p><b>Note 1 </b>: Use this method only if the source document features * a default namespace (without a prefix). You will need to map that * namespace to a prefix for queries to work.</p> * <p><b>Note 2 </b>: This method calls IE's setProperty method to set the * appropriate namespace-prefix mappings, so you dont have to do that.</p> * @param oDoc The target XMLDocument to set the namespace mappings for. * @param sNsSet A whilespace-seperated list of namespace declarations as * those would appear in an XML document. E.g.: * <code>"xmlns:xhtml='http://www.w3.org/1999/xhtml' * xmlns:'http://www.w3.org/1999/XSL/Transform'"</code> * @throws An error if the format of the given namespace declarations is bad. */ Sarissa.setXpathNamespaces = function(oDoc, sNsSet) { //oDoc._sarissa_setXpathNamespaces(sNsSet); oDoc._sarissa_useCustomResolver = true; var namespaces = sNsSet.indexOf(" ")>-1?sNsSet.split(" "):new Array(sNsSet); oDoc._sarissa_xpathNamespaces = new Array(namespaces.length); for(var i=0;i < namespaces.length;i++){ var ns = namespaces[i]; var colonPos = ns.indexOf(":"); var assignPos = ns.indexOf("="); if(colonPos == 5 && assignPos > colonPos+2){ var prefix = ns.substring(colonPos+1, assignPos); var uri = ns.substring(assignPos+2, ns.length-1); oDoc._sarissa_xpathNamespaces[prefix] = uri; }else{ throw "Bad format on namespace declaration(s) given"; }; }; }; /** * @private Flag to control whether a custom namespace resolver should * be used, set to true by Sarissa.setXpathNamespaces */ XMLDocument.prototype._sarissa_useCustomResolver = false; XMLDocument.prototype._sarissa_xpathNamespaces = new Array(); /** * <p>Extends the XMLDocument to emulate IE's selectNodes.</p> * @argument sExpr the XPath expression to use * @argument contextNode this is for internal use only by the same * method when called on Elements * @returns the result of the XPath search as a SarissaNodeList * @throws An error if no namespace URI is found for the given prefix. */ XMLDocument.prototype.selectNodes = function(sExpr, contextNode){ var nsDoc = this; var nsresolver = this._sarissa_useCustomResolver ? function(prefix){ var s = nsDoc._sarissa_xpathNamespaces[prefix]; if(s)return s; else throw "No namespace URI found for prefix: '" + prefix+"'"; } : this.createNSResolver(this.documentElement); var oResult = this.evaluate(sExpr, (contextNode?contextNode:this), nsresolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); var nodeList = new SarissaNodeList(oResult.snapshotLength); nodeList.expr = sExpr; for(var i=0;i<nodeList.length;i++) nodeList[i] = oResult.snapshotItem(i); return nodeList; }; /** * <p>Extends the Element to emulate IE's selectNodes</p> * @argument sExpr the XPath expression to use * @returns the result of the XPath search as an (Sarissa)NodeList * @throws An * error if invoked on an HTML Element as this is only be * available to XML Elements. */ Element.prototype.selectNodes = function(sExpr){ var doc = this.ownerDocument; if(doc.selectNodes) return doc.selectNodes(sExpr, this); else throw "Method selectNodes is only supported by XML Elements"; }; /** * <p>Extends the XMLDocument to emulate IE's selectSingleNodes.</p> * @argument sExpr the XPath expression to use * @argument contextNode this is for internal use only by the same * method when called on Elements * @returns the result of the XPath search as an (Sarissa)NodeList */ XMLDocument.prototype.selectSingleNode = function(sExpr, contextNode){ var ctx = contextNode?contextNode:null; sExpr += "[1]"; var nodeList = this.selectNodes(sExpr, ctx); if(nodeList.length > 0) return nodeList.item(0); else return null; }; /** * <p>Extends the Element to emulate IE's selectNodes.</p> * @argument sExpr the XPath expression to use * @returns the result of the XPath search as an (Sarissa)NodeList * @throws An error if invoked on an HTML Element as this is only be * available to XML Elements. */ Element.prototype.selectSingleNode = function(sExpr){ var doc = this.ownerDocument; if(doc.selectSingleNode) return doc.selectSingleNode(sExpr, this); else throw "Method selectNodes is only supported by XML Elements"; }; /** * <p>Returns a human readable description of the parsing error. Usefull * for debugging. Tip: append the returned error string in a <pre> * element if you want to render it.</p> * <p>Many thanks to Christian Stocker for the reminder and code.</p> * @argument oDoc The target DOM document * @returns The parsing error description of the target Document in * human readable form (preformated text) */ Sarissa.getParseErrorText = function (oDoc){ if (oDoc.documentElement.tagName == "parsererror"){ var parseErrorText = oDoc.documentElement.firstChild.data; parseErrorText += "\n" + oDoc.documentElement.firstChild.nextSibling.firstChild.data; return parseErrorText; }; }; }else if (_SARISSA_IS_IE){ // Add NodeType constants; missing in IE4, 5 and 6 if(!window.Node){ var Node = {ELEMENT_NODE: 1, ATTRIBUTE_NODE: 2, TEXT_NODE: 3, CDATA_SECTION_NODE: 4, ENTITY_REFERENCE_NODE: 5, ENTITY_NODE: 6, PROCESSING_INSTRUCTION_NODE: 7, COMMENT_NODE: 8, DOCUMENT_NODE: 9, DOCUMENT_TYPE_NODE: 10, DOCUMENT_FRAGMENT_NODE: 11, NOTATION_NODE: 12}; }; // implement importNode for IE if(!document.importNode){ /** * Implements importNode for IE using innerHTML. Main purpose it to * be able to append Nodes from XMLDocuments to the current page in * IE. * * @param oNode * the Node to import * @param bChildren * whether to include the children of oNode * @returns the imported node for further use */ document.importNode = function(oNode, bChildren){ var importNode = document.createElement("div"); if(bChildren) importNode.innerHTML = oNode.xml; else importNode.innerHTML = oNode.cloneNode(false).xml; return importNode.firstChild; }; }//if(!document.importNode) // for XSLT parameter names, prefix needed by IE _SARISSA_IEPREFIX4XSLPARAM = "xsl:"; // used to store the most recent ProgID available out of the above var _SARISSA_DOM_PROGID = ""; var _SARISSA_XMLHTTP_PROGID = ""; /** * Called when the Sarissa_xx.js file is parsed, to pick most recent * ProgIDs for IE, then gets destroyed. * @param idList an array of MSXML PROGIDs from which the most recent will be picked for a given object * @param enabledList an array of arrays where each array has two items; the index of the PROGID for which a certain feature is enabled */ function pickRecentProgID(idList, enabledList){ // found progID flag var bFound = false; for(var i=0; i < idList.length && !bFound; i++){ try{ var oDoc = new ActiveXObject(idList[i]); o2Store = idList[i]; bFound = true; for(var j=0;j<enabledList.length;j++) if(i <= enabledList[j][1]) Sarissa["IS_ENABLED_"+enabledList[j][0]] = true; }catch (objException){ // trap; try next progID }; }; if (!bFound) throw "Could not retreive a valid progID of Class: " + idList[idList.length-1]+". (original exception: "+e+")"; idList = null; return o2Store; }; // store proper progIDs _SARISSA_DOM_PROGID = pickRecentProgID(["Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XMLDOM"], [["SELECT_NODES", 2],["TRANSFORM_NODE", 2]]); _SARISSA_XMLHTTP_PROGID = pickRecentProgID(["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"], [["XMLHTTP", 4]]); _SARISSA_THREADEDDOM_PROGID = pickRecentProgID(["Msxml2.FreeThreadedDOMDocument.5.0", "MSXML2.FreeThreadedDOMDocument.4.0", "MSXML2.FreeThreadedDOMDocument.3.0"]); _SARISSA_XSLTEMPLATE_PROGID = pickRecentProgID(["Msxml2.XSLTemplate.5.0", "Msxml2.XSLTemplate.4.0", "MSXML2.XSLTemplate.3.0"], [["XSLTPROC", 2]]); // we dont need this anymore pickRecentProgID = null; //============================================ // Factory methods (IE) //============================================ // see mozilla version Sarissa.getDomDocument = function(sUri, sName){ var oDoc = new ActiveXObject(_SARISSA_DOM_PROGID); // if a root tag name was provided, we need to load it in the DOM // object if (sName){ // if needed, create an artifical namespace prefix the way Moz // does if (sUri){ oDoc.loadXML("<a" + _sarissa_iNsCounter + ":" + sName + " xmlns:a" + _sarissa_iNsCounter + "=\"" + sUri + "\" />"); // don't use the same prefix again ++_sarissa_iNsCounter; } else oDoc.loadXML("<" + sName + "/>"); }; return oDoc; }; // see mozilla version Sarissa.getXmlHttpRequest = function() { return new ActiveXObject(_SARISSA_XMLHTTP_PROGID); }; // see mozilla version Sarissa.getParseErrorText = function (oDoc) { var parseErrorText = "XML Parsing Error: " + oDoc.parseError.reason +" \n"; parseErrorText += "Location: " + oDoc.parseError.url + "\n"; parseErrorText += "Line Number " + oDoc.parseError.line ; parseErrorText += ", Column " + oDoc.parseError.linepos + ":\n"; parseErrorText += oDoc.parseError.srcText + "\n"; for(var i = 0; i < oDoc.parseError.linepos;i++) parseErrorText += "-"; parseErrorText += "^\n"; return parseErrorText; }; // see mozilla version Sarissa.setXpathNamespaces = function(oDoc, sNsSet) { oDoc.setProperty("SelectionLanguage", "XPath"); oDoc.setProperty("SelectionNamespaces", sNsSet); }; /** * Basic implementation of Mozilla's XSLTProcessor for IE. * Reuses the same XSLT stylesheet for multiple transforms * @constructor */ function XSLTProcessor(){ this.template = new ActiveXObject(_SARISSA_XSLTEMPLATE_PROGID); this.processor = null; }; /** * Impoprts the given XSLT DOM and compiles it to a reusable transform * @argument xslDoc The XSLT DOMDocument to import */ XSLTProcessor.prototype.importStylesheet = function(xslDoc){ // convert stylesheet to free threaded var converted = new ActiveXObject(_SARISSA_THREADEDDOM_PROGID); converted.loadXML(xslDoc.xml); this.template.stylesheet = converted; this.processor = this.template.createProcessor(); // (re)set default param values this.paramsSet = new Array(); }; /** * Transform the given XML DOM * @argument sourceDoc The XML DOMDocument to transform * @return The transformation result as a DOM Document */ XSLTProcessor.prototype.transformToDocument = function(sourceDoc) { this.processor.input = sourceDoc; var outDoc = new ActiveXObject(_SARISSA_DOM_PROGID); this.processor.output = outDoc; this.processor.transform(); return outDoc; }; /** * Set global XSLT parameter of the imported stylesheet * @argument nsURI The parameter namespace URI * @argument name The parameter base name * @argument value The new parameter value */ XSLTProcessor.prototype.setParameter = function(nsURI, name, value){ // nsURI is optional and cannot be null if(nsURI) this.processor.addParameter(name, value, nsURI); else this.processor.addParameter(name, value); // update updated params for getParameter if(!this.paramsSet[""+nsURI]) this.paramsSet[""+nsURI] = new Array(); this.paramsSet[""+nsURI][name] = value; }; /** * Gets a parameter if previously set by setParameter. Returns null * otherwise * @argument name The parameter base name * @argument value The new parameter value * @return The parameter value if reviously set by setParameter, null otherwise */ XSLTProcessor.prototype.getParameter = function(nsURI, name){ if(this.paramsSet[""+nsURI] && this.paramsSet[""+nsURI][name]) return this.paramsSet[""+nsURI][name]; else return null; };}/** * <p> * Factory method, used to set xslt parameters. * </p> * <p> * <b>Note </b> that this method can only work for the main stylesheet and * not any included/imported files. * </p> * @deprecated * @argument oXslDoc the target XSLT DOM Document * @argument sParamName the name of the XSLT parameter * @argument sParamValue the value of the XSLT parameter * @returns whether the parameter was set succefully */Sarissa.setXslParameter = function(oXslDoc, sParamQName, sParamValue){ try{ var params = oXslDoc.getElementsByTagName(_SARISSA_IEPREFIX4XSLPARAM+"param"); var iLength = params.length; var bFound = false; var param; if(sParamValue){ for(var i=0; i < iLength && !bFound;i++){ // match a param name attribute with the name given as // argument if(params[i].getAttribute("name") == sParamQName){ param = params[i]; // clean up the parameter while(param.firstChild) param.removeChild(param.firstChild); if(!sParamValue || sParamValue == null){ // do nothing; we've cleaned up the parameter anyway }else if(typeof sParamValue == "string"){ param.setAttribute("select", sParamValue); bFound = true; }else if(sParamValue.nodeName){ param.removeAttribute("select"); param.appendChild(sParamValue.cloneNode(true)); bFound = true; }else if (sParamValue.item(0) && sParamValue.item(0).nodeType){ for(var j=0;j < sParamValue.length;j++) if(sParamValue.item(j).nodeType) // check if this is a Node param.appendChild(sParamValue.item(j).cloneNode(true)); bFound = true; }else throw "Failed to set xsl:param "+sParamQName+" (original exception: "+e+")"; }; }; }; return bFound; }catch(e){ throw e; return false; };};// EOF
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?