<?xml version="1.0" encoding="ISO-8859-1" ?>

<?xml-stylesheet type="text/xsl" href="/dev/devedge/lib/xsl/devedge-1.00/article_en.xsl"?>
<nde:article 
  xmlns:nde="http://devedge.netscape.com/2002/de"
  url="/viewsource/2003/xslt-js/"
  xmlns="http://www.w3.org/1999/xhtml"  
  xml:lang="en">

  <nde:header>
    <nde:title>The XSLT/JavaScript Interface In Gecko</nde:title>
    <nde:category>Article</nde:category>
    <nde:authlist>
      <nde:author>
        <nde:authname>Doron Rosenberg</nde:authname>
        <nde:authaffil>Netscape Communications</nde:authaffil>
        <nde:email href="/community/feedback/">Feedback</nde:email>
      </nde:author>
    </nde:authlist>
    <nde:pubdate year="2003" month="05" day="09"/>

    <nde:channels>
      <nde:channel id="viewsource" />
      <nde:channel id="xml" />
      <nde:channel id="gecko" />
      <nde:channel id="ns-7" />
      <nde:channel id="ns-7.1" />
    </nde:channels>
  
    <nde:summary>
      Learn about Gecko's XSLT/JavaScript bindings and how to use the power of XSLT from JavaScript.
    </nde:summary>  
  
  </nde:header>

 <nde:head>
<style type="text/css">
     .sourceCode {padding-left: 5px; padding-top:5px;}        
     
     .sourceView {margin: 5px; padding: 5px; border: 1px dashed grey; font-family: Verdana,sans-serif;}
     .sourceInfo {padding-bottom: 5px; font-size: 0.9em; padding-right: 5px; border-bottom: 1px dashed gray; border-right: 1px dashed gray;}
     
     .color1 {color:green;}
     .color2 {color:blue;}
     
     .interfaceItem {margin-top:7px; margin-left:7px;}
</style>
</nde:head>

<nde:content>

  <h2>Introduction</h2>

    <p>
      With modern browsers <a href="/viewsource/2003/xslt-browser/">supporting XSLT</a>, developers can now use JavaScript
      to access the power that XSLT provides.  JavaScript can enable a web application to load XML data, process it via XSLT into
      a presentable form and then add it into an existing document.  Since the XML data loaded only contains the raw information  
      without any presentation data, it can load quickly even on dialup.
    </p>
    
    <p>
      XSLT allows the author to directly manipulate the structure of a document.  For example, it permits the rearranging and sorting of elements; it also provides more fine-grained control of the 
      resulting document's structure.
    </p>
      
    <p>
      As of <a href="http://mozilla.org/releases/">Mozilla 1.2</a>, Gecko enables JavaScript to create XSLT processors. This article covers 
      XSLT/JavaScript bindings in Gecko.  They are not available in Netscape 7.0x however are available in Netscape 7.1.
    </p>

  <h2>JavaScript/XSLT Bindings</h2>      
  
    <p>
      JavaScript can run XSLT transformations through the <code>XSLTProcessor</code> object.  Once instantiated, a 
      <code>XSLTProcessor</code> has an <code>importStylesheet</code> method that takes as an argument the XSLT stylesheet to be used 
      in the transformation.  The stylesheet has to be passed in as an XML document, which means that the .xsl file has to be loaded 
      by the page before calling <code>importStylesheet</code>.  This can be done via <code>XMLHttpRequest</code> or 
      <code>XMLDocument.load</code>.
    </p>

   <div class="sourceView">
     <span class="sourceInfo">Figure 1 : Instantiating an XSLTProcessor</span>
 
     <p class="sourceCode">
       <strong>JavaScript</strong><br /><br />
       &#160;&#160;var xsltProcessor = new XSLTProcessor();<br /><br />

       &#160;&#160;// Load the xsl file using synchronous (third param is set to false) XMLHttpRequest<br />
       &#160;&#160;var myXMLHTTPRequest = new XMLHttpRequest();<br />
       &#160;&#160;myXMLHTTPRequest.open("GET", "example.xsl", false);<br />
       &#160;&#160;myXMLHTTPRequest.send(null);<br /><br />
   
       &#160;&#160;var xslRef = myXMLHTTPRequest.responseXML;<br /><br />
       
       &#160;&#160;// Finally import the .xsl<br />
       &#160;&#160;xsltProcessor.importStylesheet(xslRef);<br />            
     </p>         
   </div>
   
   <p>
     For the actual transformation, <code>XSLTProcessor</code> requires an XML document, which is used in conjunction with the
     imported XSL file to produce the final result.  The XML document can be a separate XML file loaded as shown in figure 1, or
     it can be part of the existing page.  To process part of a page's DOM, it is necessary to first create and XML document in 
     memory.  Assuming that the DOM to be processed is contained by an element with the id <code>example</code>, that DOM can be
     "cloned" using the in-memory XML document's <code>importNode</code> method.  <code>importNode</code> allows
     transferring a DOM fragment between documents, in this case from an HTML document to an XML document.  
     The first parameter references the DOM node to clone.  By making the second parameter "true",
     it will clone all descendants as well (a deep clone).  The cloned DOM can then be easily inserted into the XML document using
     <code>appendChild</code>, as shown in figure 2.
   </p>

   <div class="sourceView">
     <span class="sourceInfo">Figure 2 : Creating an XML document based on part of a document's DOM</span>
 
     <p class="sourceCode">
       <strong>JavaScript</strong><br /><br />
       &#160;&#160;// create a new XML document in memory<br />
       &#160;&#160;var xmlRef = document.implementation.createDocument("", "", null);<br /><br />

       &#160;&#160;// we want to move a part of the DOM from an HTML document to an XML document.<br />
       &#160;&#160;// importNode is used to clone the nodes we want to process via XSLT - true makes it do a deep clone<br />
       &#160;&#160;var myNode = document.getElementById("example");<br />
       &#160;&#160;var clonedNode = xmlRef.importNode(myNode, true);<br /><br />       

       &#160;&#160;// add the cloned DOM into the XML document<br />
       &#160;&#160;xmlRef.appendChild(clonedNode);
     </p>         
   </div>
     
   <p>
     Once the stylesheet has been imported, <code>XSLTProcessor</code> has to perform two methods for the actual 
     transformation, namely <code>transformToDocument()</code> and <code>transformToFragment()</code>.  <code>transformToDocument()</code>
     returns a full XML document while <code>transformToFragment()</code> returns a document fragment that can be easily added to an
     existing document.  Both take in the XML document as the first parameter that will be transformed.
    </p>
    <p>
     <code>transformToFragment()</code> requires a second parameter, namely the document object that will own the generated fragment.  
     If the generated fragment will be inserted into the current HTML document, passing in <code>document</code> is enough.
   </p>
   
   <div class="sourceView">
     <span class="sourceInfo">Figure 3 : Performing the transformation</span>
 
     <p class="sourceCode">
       <strong>JavaScript</strong><br /><br />
       &#160;&#160;var fragment = xsltProcessor.transformToFragment(xmlRef, document);
     </p>  
   </div>

  <h2>Basic Example</h2> 
  
    <p>
      The basic example will load an XML file and apply a XSL transformation on it.  These are the same files used in the
      "Generating HTML" example in the <a href="/viewsource/2003/xslt-browser/">XSLT in Netscape Gecko</a> article.  The
      XML file describes an article and the XSL file formats the information for display.
    </p>
    
   <div class="sourceView">
     <span class="sourceInfo">Figure 4 : XML file</span>
 
     <p class="sourceCode">
       <strong>XML Document (example1.xml):</strong><br /><br />
       
        &#160;&#160;&lt;?xml version="1.0"?><br />
        &#160;&#160;&lt;<span class="color1">myNS:</span>Article<br />
        &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;xmlns:<span class="color1">myNS="http://devedge.netscape.com/2002/de</span>"><br />
        &#160;&#160;&#160;&#160;&lt;<span class="color1">myNS:</span>Title>My Article&lt;/<span class="color1">myNS:</span>Title><br />
        &#160;&#160;&#160;&#160;&lt;<span class="color1">myNS:</span>Authors><br />
        &#160;&#160;&#160;&#160;&#160;&#160;&lt;<span class="color1">myNS:</span>Author company="Foopy Corp.">Mr. Foo&lt;/<span class="color1">myNS:</span>Author><br />
        &#160;&#160;&#160;&#160;&#160;&#160;&lt;<span class="color1">myNS:</span>Author>Mr. Bar&lt;/<span class="color1">myNS:</span>Author><br />
        &#160;&#160;&#160;&#160;&lt;/<span class="color1">myNS:</span>Authors><br />
        &#160;&#160;&#160;&#160;&lt;<span class="color1">myNS:</span>Body><br />
        &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; The <span class="color2">&lt;b></span>rain<span class="color2">&lt;/b></span> in <span class="color2">&lt;u></span>Spain<span class="color2">&lt;/u></span> stays mainly in the plains.<br />
        &#160;&#160;&#160;&#160;&lt;/<span class="color1">myNS:</span>Body><br />
        &#160;&#160;&lt;/<span class="color1">myNS:</span>Article><br />
     </p>         
   </div>    

 <div class="sourceView">
     <span class="sourceInfo">Figure 5 : XSLT Stylesheet</span>
 
     <p class="sourceCode">
      <strong>XSL Stylesheet (example1.xsl):</strong><br /><br />

       &#160;&#160;&lt;?xml version="1.0"?><br />
       &#160;&#160;&lt;xsl:stylesheet version="1.0" <br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;xmlns:xsl="http://www.w3.org/1999/XSL/Transform"<br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color1">xmlns:myNS="http://devedge.netscape.com/2002/de"</span>><br /><br />
         
       &#160;&#160;&#160;&#160;&lt;xsl:output method="html" /><br /><br />       
       
       &#160;&#160;&#160;&#160;&lt;xsl:template match="/"><br />
       &#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;html></span><br /><br />

       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;head></span><br /><br />

       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;title></span><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;xsl:value-of select="/<span class="color1">myNS:Article</span>/<span class="color1">myNS:Title</span>"/><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;/title></span><br /><br />

       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;style type="text/css"><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;.myBox {margin:10px 155px 0 50px; border: 1px dotted #639ACE; padding:0 5px 0 5px;}<br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;/style></span><br /><br />

       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;/head></span><br /><br />
       
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;body></span><br />    
     
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;p class="myBox"><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;span class="title"><br /></span>
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;xsl:value-of select="/<span class="color1">myNS:Article</span>/<span class="color1">myNS:Title</span>"/><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;/span> &lt;/br></span><br /><br />
          
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">Authors: &#160;&#160;&lt;br /></span><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;xsl:apply-templates select="/<span class="color1">myNS:Article</span>/<span class="color1">myNS:Authors</span>/<span class="color1">myNS:Author</span>"/><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;/p></span><br /><br />
        
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;p class="myBox"></span><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;xsl:apply-templates select="//<span class="color1">myNS:Body</span>"/><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;/p></span><br /><br />
      
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;/body><br /><br />
      
       &#160;&#160;&#160;&#160;&#160;&#160;&lt;/html></span><br />
       &#160;&#160;&#160;&#160;&lt;/xsl:template><br /><br />
       
       &#160;&#160;&#160;&#160;&lt;xsl:template match="<span class="color1">myNS:Author</span>"><br />
       &#160;&#160;&#160;&#160;&#160;&#160; -- &#160;&#160;&lt;xsl:value-of select="." /><br /><br /> 
      
       &#160;&#160;&#160;&#160;&#160;&#160;&lt;xsl:if test="@company"><br />
       &#160;&#160;&#160;&#160;&#160;&#160; :: &#160;&#160;<span class="color2">&lt;b></span>&#160;&#160;&lt;xsl:value-of select="@company" />&#160;&#160;<span class="color2">&lt;/b></span><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&lt;/xsl:if><br /><br />

       &#160;&#160;&#160;&#160;&#160;&#160;<span class="color2">&lt;br /></span><br />
       &#160;&#160;&#160;&#160;&lt;/xsl:template><br /><br />

       &#160;&#160;&#160;&#160;&lt;xsl:template  match="<span class="color1">myNS:Body</span>"><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&lt;xsl:copy><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;xsl:apply-templates select="@*|node()"/><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&lt;/xsl:copy><br />
       &#160;&#160;&#160;&#160;&lt;/xsl:template><br /><br />
              
       &#160;&#160;&#160;&#160;&lt;xsl:template match="@*|node()"><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;xsl:copy><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;xsl:apply-templates select="@*|node()"/><br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;/xsl:copy><br />
       &#160;&#160;&#160;&#160;&lt;/xsl:template><br />
       &#160;&#160;&lt;/xsl:stylesheet>
     </p>         
   </div>   
   
   <p>
     The example loads using synchronous XMLHTTPRequest both the .xsl (<code>xslStylesheet</code>) and the .xml (<code>xmlDoc</code>) 
     files into memory.  The .xsl file is then imported (<code>xsltProcessor.importStylesheet(xslStylesheet)</code>) 
     and the transformation run (<code>xsltProcessor.transformToFragment(xmlDoc, document)</code>).  This allows fetching of
     data after the page has been loaded, without initiating a fresh page load.
   </p>
   
   <div class="sourceView">
     <span class="sourceInfo">Figure 6 : Example - <a href="example1.html">view example</a></span>
 
     <p class="sourceCode">
       <strong>JavaScript:</strong><br /><br />
         var xslStylesheet;<br />
         var xsltProcessor = new XSLTProcessor();<br />
         var myDOM;<br /><br />
    
         var xmlDoc;<br /><br />
 
         function Init(){<br /><br />

          &#160;&#160;// load the xslt file, example1.xsl<br />
          &#160;&#160;var myXMLHTTPRequest = new XMLHttpRequest();<br />
          &#160;&#160;myXMLHTTPRequest.open("GET", "example1.xsl", false);<br />
          &#160;&#160;myXMLHTTPRequest.send(null);<br /><br />
   
          &#160;&#160;xslStylesheet = myXMLHTTPRequest.responseXML;<br />
          &#160;&#160;xsltProcessor.importStylesheet(xslStylesheet);<br /><br />

          &#160;&#160;// load the xml file, example1.xml<br />
          &#160;&#160;myXMLHTTPRequest = new XMLHttpRequest();<br />
          &#160;&#160;myXMLHTTPRequest.open("GET", "example1.xml", false);<br />
          &#160;&#160;myXMLHTTPRequest.send(null);<br /><br />
      
          &#160;&#160;xmlDoc = myXMLHTTPRequest.responseXML;<br /><br />
            
          &#160;&#160;var fragment = xsltProcessor.transformToFragment(xmlDoc, document);<br /><br />

          &#160;&#160;document.getElementById("example").innerHTML = "";<br /><br />

          &#160;&#160;myDOM = fragment;<br />
          &#160;&#160;document.getElementById("example").appendChild(fragment);<br />
        }       
     </p>         
   </div>     

  <h2>Setting Parameters</h2>
  
    <p>
      While running transformations using precoded .xsl and .xml files is quite useful, configuring 
      the .xsl file from JavaScript may be even more useful.  For example, JavaScript and XSLT could be used to sort XML data and then display it.  The sorting
      would have to alternate between ascending and descending sorting. 
    </p>
    
    <p>
      XSLT provides the <code>xsl:param</code> element, which is a child of the <code>xsl:stylesheet</code> element.  
      <code>XSLTProcessor()</code> provides three JavaScript methods to interact with these parameters: 
      <code>setParameter</code>, <code>getParameter</code> and <code>removeParameter</code>. They all take as the first argument the namespace URI of the <code>xsl:param</code>
      (Usually the param will fall in the default namespace, so passing in "null" will suffice.) The local name of the <code>xsl:param</code>
      is the second argument.  <code>setParameter</code> requires a third argument - namely the value to which the parameter will be set.
      
    </p>

   <div class="sourceView">
     <span class="sourceInfo">Figure 7 : Parameters</span>
 
     <p class="sourceCode">
       <strong>XSLT:</strong><br /><br />              
       &lt;xsl:param name="myOrder" /><br /><br />
       
       <strong>JavaScript:</strong><br /><br />
       var sortVal = xsltProcessor.getParameter(null, "myOrder");<br /><br />

       if (sortVal == "" || sortVal == "descending")<br />
         &#160;&#160;xsltProcessor.setParameter(null, "myOrder", "ascending");<br />
       else<br />
         &#160;&#160;xsltProcessor.setParameter(null, "myOrder", "descending");<br />
     </p>         
   </div>     

   
  <h2>Advanced Example</h2>
  
    <p>
      The advanced example will sort several divs based on their content.  The example allows to sort the content multiple times,
      alternating between ascending and descending sorting.  The JavaScript only loads the .xsl file the first time, and sets the
      <code>xslloaded</code> variable to <code>true</code> once it has finished loading the file.  Using the <code>getParameter</code> method
      on the <code>XSLTProcessor</code> object, the code can figure wether to sort by ascending or descending.  It defaults to 
      ascending if the parameter is empty (first time the sorting happens, as there is no value for it in the XSLT file).  The sorting
      value is set using <code>setParameter</code>.
    </p>
    
    <p>
      The XSLT file has a parameter called <code>myOrder</code> that JavaScript sets to change the sorting method.  The <code>xsl:sort</code>
      element's <code>order</code> attribute can access the value of the parameter using <code>$myOrder</code>.  However, the value needs to 
      be an XPATH expression and not a string, so <code>{$myOrder}</code> is used.  Using {} evaluates the content as an XPath expression.
    </p>
    
    <p>
      Once the transformation is complete, the result is appened to the document, as shown in this <a href="example2.html">example</a>.
    </p>
    
    
   <div class="sourceView">
     <span class="sourceInfo">Figure 7 : Sorting based on div content - <a href="example2.html">view example</a></span>
 
     <p class="sourceCode">
       <strong>XHTML Fragment (example1.html):</strong><br /><br />
       &lt;div id="example"><br />
         &#160;&#160;&lt;div>1&lt;/div><br />
         &#160;&#160;&lt;div>2&lt;/div><br />
         &#160;&#160;&lt;div>3&lt;/div><br />
         &#160;&#160;&lt;div>4&lt;/div><br />
         &#160;&#160;&lt;div>5&lt;/div><br />
         &#160;&#160;&lt;div>6&lt;/div><br />
         &#160;&#160;&lt;div>7&lt;/div><br />
         &#160;&#160;&lt;div>8&lt;/div><br />
         &#160;&#160;&lt;div>9&lt;/div><br />
         &#160;&#160;&lt;div>10&lt;/div><br />
       &lt;/div><br /><br />     
     
       <strong>JavaScript</strong><br /><br />
       var xslRef;<br />
       var xslloaded = false;<br />
       var xsltProcessor = new XSLTProcessor();<br />
       var myDOM;<br /><br />
    
       var xmlRef = document.implementation.createDocument("", "", null);<br /><br />

       function sort() {<br />
         &#160;&#160;if (!xslloaded){<br />
           &#160;&#160;&#160;&#160;p = new XMLHttpRequest();<br />
           &#160;&#160;&#160;&#160;p.open("GET", "example2.xsl", false);<br />
           &#160;&#160;&#160;&#160;p.send(null);<br /><br />
   
           &#160;&#160;&#160;&#160;xslRef = p.responseXML;<br />
           &#160;&#160;&#160;&#160;xsltProcessor.importStylesheet(xslRef)<br />
           &#160;&#160;&#160;&#160;xslloaded = true;<br />
         &#160;&#160;}<br /><br />
      
         &#160;&#160;// create a new XML document in memory<br />
         &#160;&#160;xmlRef = document.implementation.createDocument("", "", null);<br /><br />

         &#160;&#160;// we want to move a part of the DOM from an HTML document to an XML document.<br />
         &#160;&#160;// importNode is used to clone the nodes we want to process via XSLT - true makes it do a deep clone<br />
         &#160;&#160;var myNode = document.getElementById("example");<br />
         &#160;&#160;var clonedNode = xmlRef.importNode(myNode, true);<br /><br />
      
         &#160;&#160;// after cloning, we append<br />
         &#160;&#160;xmlRef.appendChild(clonedNode);<br /><br />    
        
         &#160;&#160;// set the sorting parameter in the XSL file<br />
         &#160;&#160;var sortVal = xsltProcessor.getParameter(null, "myOrder");<br /><br />

         &#160;&#160;if (sortVal == "" || sortVal == "descending")<br />
           &#160;&#160;&#160;&#160;xsltProcessor.setParameter(null, "myOrder", "ascending");<br />
         &#160;&#160;else<br />
           &#160;&#160;&#160;&#160;xsltProcessor.setParameter(null, "myOrder", "descending");<br /><br />

         &#160;&#160;// initiate the transformation<br />
         &#160;&#160;var fragment = xsltProcessor.transformToFragment(xmlRef, document);<br /><br />
             
         &#160;&#160;// clear the contents<br />
         &#160;&#160;document.getElementById("example").innerHTML = "";<br /><br />

         &#160;&#160;myDOM = fragment;<br />
         &#160;&#160;// add the new content from the transformation<br />
         &#160;&#160;document.getElementById("example").appendChild(fragment)<br />
       }<br /><br />
       
        <strong>XSL Stylesheet (example2.xsl):</strong><br /><br />
          &lt;?xml version="1.0" encoding="UTF-8"?><br />
          &lt;stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><br />
          &#160;&#160;&lt;xsl:output method="html" indent="yes"/><br /><br />

          &#160;&#160;&lt;xsl:param name="myOrder" /><br /><br />

          &#160;&#160;&lt;xsl:template match="/"><br /><br />
  
            &#160;&#160;&#160;&#160;&lt;xsl:apply-templates select="/div//div"><br />
              &#160;&#160;&#160;&#160;&#160;&#160;&lt;xsl:sort select="." data-type="number" order="{$myOrder}"/><br />
            &#160;&#160;&#160;&#160;&lt;/xsl:apply-templates><br />
          &#160;&#160;&lt;xsl:template><br /><br />

          &#160;&#160;&lt;xsl:template match="div"><br />
            &#160;&#160;&#160;&#160;&lt;xsl:copy-of select="."/><br />
          &#160;&#160;&lt;xsl:template><br />
        &lt;/xsl:stylesheet>
     </p>         
   </div>

  <h2>Interface List</h2>

  <h3>XSLTProcessor</h3>
    Methods

      <dt class="interfaceItem"><strong>void importStylesheet(DOMNode styleSheet)</strong></dt>
      <dd>imports the XSLT stylesheet.  <code>styleSheet</code> is the root-node of a XSLT stylesheet.</dd>
    
      <dt class="interfaceItem"><strong>DOMDocumentFragment transformToFragment(DOMNode source, DOMDocument owner)</strong></dt>
      <dd>transforms the node <code>source</code> by applying the stylesheet imported using the importStylesheet() function. The owner 
      document of the resulting document fragment is the <code>owner</code> node.</dd>
    
      <dt class="interfaceItem"><strong>DOMDocument transformToDocument(DOMNode source)</strong></dt>
      <dd>transforms the node <code>source</code> applying the stylesheet given importing using the importStylesheet() function.</dd>

      <dt class="interfaceItem"><strong>void setParameter(String namespaceURI, String localName, Variant value)</strong></dt>
      <dd>sets a parameter in the XSLT stylesheet that was imported.</dd>

      <dt class="interfaceItem"><strong>Variant getParameter(String namespaceURI, String localName)</strong></dt>
      <dd>gets the value of a parameter from the XSLT stylesheet.</dd>

      <dt class="interfaceItem"><strong>void removeParameter(String namespaceURI, String localName)</strong></dt>
      <dd>removes the parameter if it was previously set.  This will make the XSLTProcessor use the default value for the parameter as 
      specified in the stylesheet.</dd>
            
      <dt class="interfaceItem"><strong>void clearParameters()</strong></dt>
      <dd>removes all set parameters from the XSLTProcessor.  The XSLTProcessor will then use the defaults specified in the XSLT stylesheet.</dd>
      
      <dt class="interfaceItem"><strong>void reset()</strong></dt>
      <dd>removes all parameters and stylesheets from the XSLTProcessor.</dd>

   
  <h2>Resources</h2>    
     <ul>
       <li><a href="/viewsource/2003/xslt-browser/">XSLT in Netscape Gecko</a></li>
       <li><a href="http://www.mozilla.org/projects/xslt/js-interface.html">Using the Mozilla JavaScript interface to do XSL Transformations</a> at <a href="http://www.mozilla.org/">mozilla.org</a>.</li>
       <li><a href="http://www.mozilla.org/projects/xslt/">Mozilla.org's XSLT Project Page</a>, which includes a frequently encountered issues section.</li>
       <li><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmconusingthexslprocessor.asp">MSDN documentation on IE/XSLT bindings</a></li>
     </ul>    
  </nde:content>

  <nde:related area="nde">
    <nde:item url="/central/xml">XML Central</nde:item>
    <nde:item url="/viewsource/2003/xslt-browser/">XSLT in Netscape Gecko</nde:item>    
    <nde:item url="/library/releases/netscape-7.1/">Other Articles about Netscape 7.1</nde:item>
  </nde:related> 
</nde:article>
