📄 overview-summary.html
字号:
You can chack for and get a human-readable description of the error using the Sarissa getParseErrorText method, passing the document as the argument: </p> <pre>if(Sarissa.getParseErrorText(oDomDoc) == Sarissa.PARSED_OK){ // The document was parsed/loaded just fine, go on doSomething(oDomDoc);} else{ // The document was not loaded correctly! Inform the user: alert(Sarissa.getParseErrorText(oDomDoc));};</pre> <p> Sarissa.getParseErrorText will return one of: </p> <ul> <li><code>Sarissa.PARSED_OK</code> if the document was parsed with no errors</li> <li><code>Sarissa.PARSED_EMPTY</code> if the document is empty (this may occur instead of an error using XmlHttpRequest)</li> <li><code>Sarissa.PARSED_UNKNOWN_ERROR</code> if the document was not loaded for an unknown reason</li> <li>A human readable description of the parsing error</li> </ul> <p> <span class="bold">Tip</span>: Wrap the result of Sarissa.getParseErrorText in a <code>"pre"</code> element if you want to render it. </p> <p> If you have used the deprecated .load methods, you can also use the <code>parseError</code> property. The property always gives an integer, anything other than zero signals an error. </p> <pre xml:space="preserve">// ...oDomDoc.async = false;oDomDoc.load("someDocument.xml");if(oDomDoc.parseError.errorCode != 0) alert("not well formed or other error!");else alert("loaded ok");</pre> </div> <div class="section"> <h3> <a id="transform">How to transform a DOM Document Object with XSLT</a> </h3> <p> Both Mozilla and IE provide JavaScript interfaces to control XSLT transformations thanks to Transformiix and MSXML3 (or above) respectively. MSXML3+ is available with IE6 or as a seperate instalation. </p> <p> Sarissa emulates Mozilla's XSLTProcessor for Internet Explorer. This object allows reusability of stylsheets; with it you can use the same stylesheet on more than one source file. You use the XSLTProcessor to control transformations and set / get stylesheet parameters as in the following example: </p> <pre xml:space="preserve">// create an instance of XSLTProcessorvar processor = new XSLTProcessor();// create a DOM Document containing an XSLT stylesheetvar xslDoc = Sarissa.getDomDocument();var xslStr = "<?xml version='1.0' encoding='UTF-8'?>"+ "<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >"+ "<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>"+ "<xsl:param name='title'><xsl:value-of select=\"'default title'\"/></xsl:param>"+ "<xsl:template match='/'><p class='test' title='{$title}'>test</p>"+ "</xsl:template><xsl:template match='@*'>"+ "</xsl:template></xsl:stylesheet>";xslDoc = (new DOMParser()).parseFromString(xslStr, "text/xml");// make the stylesheet reusable by importing it in the // XSLTProcessorprocessor.importStylesheet(xslDoc);// now XSLTProcessor is the 'proxy' for our stylesheet,// the function below demonstrates usagefunction test(paramValue) { // set a stylesheet parameter processor.setParameter(null, "title", paramValue); // create source document var xmlDoc = Sarissa.getDomDocument("http://foo.org/ns/uri","foo", null); // transform the document var newDocument = processor.transformToDocument(xmlDoc); // show transformation results alert(new XMLSerializer().serializeToString(newDocument));}// test the above functiontest("test 1");</pre> </div> <div class="section"> <h3> <a id="updateContentFromURI">How to update areas in your webpage from remote (optionally transformed) XML documents or XML DOM Nodes (oneliner!)</a> </h3> <p> Loading a DOM document from a URL, transforming it and using it to update the content of an HTML element is a very common task if you are into "AJAX" apps. With Sarissa you can do it in a single line of code: </p> <pre xml:space="preserve"> Sarissa.updateContentFromURI(sFromUrl, oTargetElement, xsltproc, callback, skipCache);</pre> <p> The parameters used here are: </p> <ol> <li><code>sFromUrl</code>: the URL to make the request to, e.g. "http://localhost/mydoc.xml"</li> <li><code>oTargetElement</code>: the element to update, e.g. <code>document.getElementById('content')</code></li> <li><code>xsltproc</code> (optional): the transformer to use on the XML Document before updating the target element with it </li> <li><code>callback</code> (optional): a Function object to execute once the update is finished successfuly, called as callback(oNode, oTargetElement)</li> <li><code>skipCache</code> (optional): whether to skip any cache</li> </ol> <p>Quite similarly, if you want to use an XML DOM object instead of a remote XML document simply take a look at <code>Sarissa.updateContentFromNode</code></p> </div> <div class="section"> <h3> <a id="xpath">How to use XPath from JavaScript to select Nodes from an XML Document</a> </h3> <p> Mozilla fully implements <a href="http://www.w3.org/TR/DOM-Level-3-XPath">DOM Level 3 XPath</a> so it was pretty trivial to implement IE's basic <code>selectNodes</code> and <code>selectSingleNode</code> methods, with full namespaces support. This is available in sarissa_ieemu_xpath.js. Actually IE also needs the proprietary <code>setProperty</code> method for it's XPath implementation to work. <code>setProperty</code> is used for a number of things in IE. First you'll have to use it to make XPath available for a certain document: </p> <pre xml:space="preserve">oDomDoc.setProperty("SelectionLanguage", "XPath");</pre> <p>In IE, using <code>selectNodes</code> or <code>selectSingleNode</code> without the above first will give an error. Also, the same method with different parameters is used to allow IE to resolve namespace prefixes, for example:</p> <pre xml:space="preserve">oDomDoc.setProperty("SelectionNamespaces", "xmlns:xhtml='http://www.w3.org/1999/xhtml'");</pre> <p>If you want to allow IE to resolve multiple namespace prefixes, use a space delimited list like:</p> <pre xml:space="preserve">oDomDoc.setProperty("SelectionNamespaces", "xmlns:xhtml='http://www.w3.org/1999/xhtml' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");</pre> <p>Mozilla does not need any of the above. DOM L3 XPath is always available and namespaces are resolved err... automatically. Below is an example of using <code>selectNodes</code> and <code>selectSingleNode</code> when Sarissa is available to provide cross browser XPath functionality. For more documentation on these proprietary methods check with the documentation at the MSDN website (<a href="http://msdn.microsoft.com" title="link to Microsoft developer website">http://msdn.microsoft.com</a>). I'm not providing a URL for that as they constantly change their URLs.</p> <pre xml:space="preserve"><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head> <title>Untitled</title> <script type="text/javascript" src="sarissa.js"> </script> <script type="text/javascript" src="sarissa_ieemu_xpath.js"> </script> <script type="text/javascript"> <!--function testXpath(){ var xmlDoc = Sarissa.getDomDocument(); var objNodeList; var xmlStr = "<?xml version='1.0' encoding='UTF-8'?>"+ "<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>"+ "<xsl:output method='xml' version='1.0' encoding='UTF-8' indent='yes'/>"+ "<xsl:template match='*'></xsl:template><xsl:template match='@*'>"+ "</xsl:template></xsl:stylesheet>"; xmlDoc = (new DomParser()).parseFromString(xmlStr, "text/xml"); <span style="bold">// the following two lines are needed for IE</span> xmlDoc.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'"); xmlDoc.setProperty("SelectionLanguage", "XPath"); testSelectNodesOn(xmlDoc, "//xsl:template"); testSelectNodesOn(xmlDoc.documentElement, "//xsl:template"); testSelectNodesOn((xmlDoc.documentElement.getElementsByTagName("*"))[0], "//xsl:template");}function testSelectNodesOn(domNode, sXpath){ alert("testing selectNodes("+sXpath+") on a "+domNode); var objNodeList = domNode.selectNodes(sXpath); for(i=0;i<objNodeList.length;i++) alert(new XMLSerializer().serializeToString(objNodeList[i])); alert("testing selectSingleNode("+sXpath+") on a "+domNode); var oElem = domNode.selectSingleNode(sXpath); alert(oElem+"\n"+new XMLSerializer().serializeToString(oElem));}; //--> </script></head><body><button onclick="testXpath()">test xpath</button></body></html></pre> </div> <div class="section"> <h3> <a id="nondom2xml">How to serialize non-DOM objects to XML </a> </h3> <p> You can easily convert any non DOM object to XML using the <code>Sarissa.xmlize</code> method. Sarissa will preserve the structure and naming of the object graph, translating it to an XML tree. Collection items are translated to <code>array-item</code> elements. For an example, the following lines: </p> <pre xml:space="preserve">// create an object hierarchy book.chapters = new Array();book.chapters[0] = "Kingdom of Tags";book.chapters[1] = "Fall";book.chapters[2] = "Final battle"; book.chapters[3] = "Characters that need to be escaped: << << \"' \"\"\"&&'' < > & ' \" ";book.chapters[4] = "Epilogue";book.editor = "Manos Batsis";var publisher = new Object();publisher.name = "Some Publisher";book.publisher = publisher;// serialize to an XML stringvar s = Sarissa.xmlize(book, "book");alert("Generated XML:\n"+s)</pre> <p>will generate the markup below:</p> <pre xml:space="preserve"><book> <chapters> <array-item key="0">Kingdom of fools</array-item> <array-item key="1">Fall</array-item> <array-item key="2">Final battle</array-item> <array-item key="3"> Characters that need to be escaped: &lt;&lt; &lt;&lt; &quot;&apos; &quot;&quot;&quot;&amp;&amp;&apos;&apos; &lt; &gt; &amp; &apos; &quot; </array-item> <array-item key="4">Epilogue</array-item> </chapters> <editor>Manos Batsis</editor> <publisher> <name>Some Publisher</name> </publisher></book></pre> </div> <p> <a href="http://sourceforge.net"> <img src="http://sourceforge.net/sflogo.php?group_id=75155&type=1" width="88" height="31" alt="SourceForge.net Logo" /> </a> </p></div> </p><hr> <table border="1" cellpadding="3" cellspacing="0" width="100%"> <tr bgcolor="#CCCCFF" class="TableHeadingColor"> <td colspan=2><font size="+2"> <b>File Summary</b> </font></td> </tr> <tr bgcolor="#eeeeee" class="TableRowColor"> <td width="15%"><b><a href="overview-summary-sarissa.js.html">sarissa.js</a></b></td> <td> </td> </tr> <tr bgcolor="#eeeeee" class="TableRowColor"> <td width="15%"><b><a href="overview-summary-sarissa_dhtml.js.html">sarissa_dhtml.js</a></b></td> <td> </td> </tr> <tr bgcolor="#eeeeee" class="TableRowColor"> <td width="15%"><b><a href="overview-summary-sarissa_ieemu_load.js.html">sarissa_ieemu_load.js</a></b></td> <td> </td> </tr> <tr bgcolor="#eeeeee" class="TableRowColor"> <td width="15%"><b><a href="overview-summary-sarissa_ieemu_xpath.js.html">sarissa_ieemu_xpath.js</a></b></td> <td> </td> </tr> <tr bgcolor="#eeeeee" class="TableRowColor"> <td width="15%"><b><a href="overview-summary-sarissa_ieemu_xslt.js.html">sarissa_ieemu_xslt.js</a></b></td> <td> </td> </tr> </table> <hr/> <!-- ========== METHOD SUMMARY =========== --><!-- ========== END METHOD SUMMARY =========== --><!-- ========== START OF NAVBAR ========== --><a name="navbar_top"><!-- --></a><table border="0" width="100%" cellpadding="1" cellspacing="0"><tr><td colspan=2 bgcolor="#b8cade" class="NavBarCell1"><a name="navbar_top_firstrow"><!-- --></a><table border="0" cellpadding="0" cellspacing="3"> <tr align="center" valign="top"> <td bgcolor="#FFFFFF" class="NavBarCell1Rev"> <font class="NavBarFont1Rev"><b>Overview</b></font> </td> <td bgcolor="#b8cade" class="NavBarCell1"> <font class="NavBarFont1">File</font> </td> <td bgcolor="#FFFFFF" class="NavBarCell1"> <font class="NavBarFont1">Class</font> </td> <td bgcolor="#b8cade" class="NavBarCell1"> <a href="overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a> </td> <td bgcolor="#b8cade" class="NavBarCell1"> <a href="index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a> </td> <td bgcolor="#b8cade" class="NavBarCell1"> <a href="help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a> </td> </tr></table></td><td bgcolor="#b8cade" align="right" valign="top"><em><b>sarissa</b></em></td></tr><tr><td bgcolor="#eeeeee" class="NavBarCell2"><font size="-2"> PREV NEXT</font></td><td bgcolor="#eeeeee" class="NavBarCell2"><font size="-2"> <a href="index.html" target="_top"><b>FRAMES</b></a> <a href="overview-summary.html" target="_top"><b>NO FRAMES</b></a> <script> <!-- if(window==top) { document.writeln('<A HREF="allclasses-noframe.html" TARGET=""><B>All Classes</B></A>'); } //--></script><noscript><a href="allclasses-noframe.html" target=""><b>All Classes</b></a></noscript></font></td></tr></table><!-- =========== END OF NAVBAR =========== --><hr><font size="-1"></font><div class="jsdoc_ctime">Documentation generated by <a href="http://jsdoc.sourceforge.net/" target="_parent">JSDoc</a> on Thu Nov 30 22:06:11 2006</div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -