MSXML2.FreeThreadedDOMDocument

文档说明  

   
  Choosing   a   Threading   Model  
   
   
  There   are   two   versions   of  the   XML   control.    
   
  rental   model   
  The   rental   model   version   is  designed   for  single-threaded   access.  To   use   the  rental   model  control,   use  the   "Microsoft.DOMDocument"  progID.    
  free-threaded   model   
  The   free-threaded  version   is   designed   for   multiple  thread   access.  To   use   the  free-threaded   control,  use   the   "Microsoft.FreeThreadedDOMDocument"   progID.   
  If   you   plan   for   several  threads   to   access   your   XML  data   from   a   single   control,  be   sure   to  use   the   free-threaded   control.   If  only   one   thread   will   access  the   XML   data,   use   the  rental   model  control   for  better   performance. 
   
  The   following  is   a   sample   global.asa   file  that   creates  session-level   and  application-level   free-threaded   versions   of  the   XML   control.  
   
  <SCRIPT   LANGUAGE=VBScript  RUNAT=Server>  
      Sub  Session_OnStart  
          ON   error  RESUME   next 
             
          SET   Application("AppFXMLdoc")   =   _ 
              server.CreateObject("MSXML2.FreeThreadedDOMDocument") 
          SET   Session("SessFXMLdoc")   =   _ 
              server.CreateObject("MSXML2.FreeThreadedDOMDocument") 
      End  Sub  
      Sub  Session_OnEnd  
          ON   error  RESUME   next 
               
          SET   Session("SessFXMLdoc")   =   nothing  
          SET   Application("AppFXMLdoc")   =   nothing  
          Session("SessFXMLdoc")  =   empty 
          Application("AppFXMLdoc")  =   empty 
      End  Sub  
  </SCRIPT>  
  Scripts   accessing  the   Session  and   Application  objects   will  be   able   to  simultaneously   access  the   "AppFXMLdoc"  and   "SessFXMLdoc"  objects.     
 
 
 
 


现在数据岛被越来越广泛的应用;其中必然会涉及到的就是动态排序了;下面列出两种实现方法:

1、为<xsl:param>标签设定参数实现动态排序(推荐)
<html>
<body>
<table DATASRC="#catalogs" border=1>
<thead>
<tr>
<tdonclick="sort('TITLE','descending');">TITLE</td>
<td>ARTIST</td>
<tdonclick="sort('COUNTRY','ascending');">COUNTRY</td>
<td>COMPANY</td>
<td>PRICE</td>
<td>YEAR</td>
</tr>
</thead>
<tbody>
<tr>
<td ><div DATAFLD="TITLE"></div></td>
<td ><divDATAFLD="ARTIST"></div></td>
<td ><divDATAFLD="COUNTRY"></div></td>
<td ><divDATAFLD="COMPANY"></div></td>
<td ><div DATAFLD="PRICE"></div></td>
<td ><div DATAFLD="YEAR"></div></td>
</tr>
</tbody>
</table>

<xml id="catalogs">
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
Bob Dylan
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Burlesque</TITLE>
Dylan
<COUNTRY>UA</COUNTRY>
<COMPANY>umbia</COMPANY>
<PRICE>1.90</PRICE>
<YEAR>1987</YEAR>
</CD>
<CD>
<TITLE>Empire</TITLE>
Bob
<COUNTRY>US</COUNTRY>
<COMPANY>bia</COMPANY>
<PRICE>12.90</PRICE>
<YEAR>1995</YEAR>
</CD>
</CATALOG>
</xml>

<xml id="xstyle">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="1.0">
<xsl:output method="xml"/>
<xsl:param name="sortorder" select="'descending'"/>
<xsl:param name="sortfield" select="'xxx'"/>
<xsl:template match="/">
<CATALOG>
<xsl:for-each select="CATALOG/CD">
<xsl:sort select="*[name()=$sortfield]" order="{$sortorder}"/>
<CD>
<TITLE><xsl:value-ofselect="TITLE"/></TITLE>
<xsl:value-of select="ARTIST"/>
<COUNTRY><xsl:value-ofselect="COUNTRY"/></COUNTRY>
<COMPANY><xsl:value-ofselect="COMPANY"/></COMPANY>
<PRICE><xsl:value-ofselect="PRICE"/></PRICE>
<YEAR><xsl:value-of select="YEAR"/></YEAR>
</CD>
</xsl:for-each>
</CATALOG>
</xsl:template>
</xsl:stylesheet>
</xml>

<scriptlanguage="vbscript">
function sort(strSortField, strSortOrder)

  dim objXSL, objXML,objTemplate, objProcessor, strHTML, strDrinkType
 
  Set objXML =CreateObject("Msxml2.FreeThreadedDOMDocument")
  Set objXSL =CreateObject("Msxml2.FreeThreadedDOMDocument")
  
  'Load the XML document
  objXML.async = False
  objXML.Loadxmlcatalogs.xml  
  
  'Load the XSL document
  objXSL.async = False
  objXSL.Loadxml xstyle.xml
     
  'Create an instance of our XSLTemplate object
  Set objTemplate =CreateObject("MSXML2.XSLTemplate")
     
  'Create an instance of ourstylesheet object using our recently loaded XSLT document
  Set objTemplate.stylesheet =objXSL
      
  'Create an instance of ourProcessor object
  Set objProcessor =objTemplate.createProcessor

  'Define the input object forour object equal to our recently loaded XML document
  objProcessor.input = objXML

  'Now, finally we can add anyparameters that we require to our Template processor
  objProcessor.AddParameter"sortfield", strSortField
  objProcessor.AddParameter"sortorder", strSortOrder
     
  'Last but not least we do ourtransformation
  objProcessor.Transform
     
  'Store the results of theoutput into a string.
  strXML =objProcessor.output

  'Load up an XML DOM object fromthe recent XML output
  objXML.loadxml strXML
  
  'Select only the "employees"elements from our document object
  objXML.selectNodes("//CATALOG")
  
  'Load our Data Island using ournew XML object
  catalogs.loadxml objXML.xml  
 end function  
</script>

<scriptlanguage="javascript">
function sort2(xmlObj, xslObj, sortByColName)
{
var xmlData=eval_r("document.all."+xmlObj).XMLDocument;
var xslData=eval_r("document.all."+xslObj).XMLDocument;
varnodes=xslData.documentElement.selectSingleNode("xsl:for-each");
nodes.selectSingleNode("@order-by").value=sortByColName;

xmlData.documentElement.transformNodeToObject(xslData.documentElement,xmlData);
}
</script>

</body>
</html>

2、直接通过DOM实现,灵活性欠缺:
<html>
<body>
<table DATASRC="#catalogs" border=1>
<thead>
<tr>
<tdonclick="sort('catalogs','xstyle','TITLE');">TITLE</td>
<td>ARTIST</td>
<td>COUNTRY</td>
<td>COMPANY</td>
<td>PRICE</td>
<td>YEAR</td>
</tr>
</thead>
<tbody>
<tr>
<td ><div DATAFLD="TITLE"></div></td>
<td ><divDATAFLD="ARTIST"></div></td>
<td ><divDATAFLD="COUNTRY"></div></td>
<td ><divDATAFLD="COMPANY"></div></td>
<td ><div DATAFLD="PRICE"></div></td>
<td ><div DATAFLD="YEAR"></div></td>
</tr>
</tbody>
</table>

<xml id='catalogs'>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
Bob Dylan
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Burlesque</TITLE>
Dylan
<COUNTRY>UA</COUNTRY>
<COMPANY>umbia</COMPANY>
<PRICE>1.90</PRICE>
<YEAR>1987</YEAR>
</CD>
<CD>
<TITLE>Empire</TITLE>
Bob
<COUNTRY>US</COUNTRY>
<COMPANY>bia</COMPANY>
<PRICE>12.90</PRICE>
<YEAR>1995</YEAR>
</CD>
</CATALOG>
</xml>

<xml id="xstyle">
<CATALOG>
<xsl:for-each select="CD" order-by="+TITLE" xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<CD>
<TITLE><xsl:value-ofselect="TITLE"/></TITLE>
<xsl:value-of select="ARTIST"/>
<COUNTRY><xsl:value-ofselect="COUNTRY"/></COUNTRY>
<COMPANY><xsl:value-ofselect="COMPANY"/></COMPANY>
<PRICE><xsl:value-ofselect="PRICE"/></PRICE>
<YEAR><xsl:value-of select="YEAR"/></YEAR>
</CD>
</xsl:for-each>
</CATALOG>
</xml>

<script language="javascript">
function sort(xmlObj, xslObj, sortByColName)
{
var xmlData=eval_r("document.all."+xmlObj).XMLDocument;
var xslData=eval_r("document.all."+xslObj).XMLDocument;
varnodes=xslData.documentElement.selectSingleNode("xsl:for-each");
nodes.selectSingleNode("@order-by").value=sortByColName;

xmlData.documentElement.transformNodeToObject(xslData.documentElement,xmlData);
}
</script>
</body>
</html>

注意当前节点的写法:
[html]<xml id="xmlsrc">
        <?xml version="1.0" encoding="GB2312"?>
        <?xml-stylesheet type="text/xsl"href="test.xsl" ?>
        <宝艾 时间="2003-9-25 11:17">
                <测试>亲爱的,能行吗?</测试>
                <测试>第二个亲爱的,能行吗?</测试>
                <测试>第三个亲爱的,能行吗?</测试>
        </宝艾>
</xml>

<xml id="xslsrc">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="1.0">
        <xsl:output method="html"/>
        <xsl:param name="param1"/>
        <xsl:template match="/">
文档创建时间: <xsl:value-of select="//@时间"/>
                <br/><br/>
                <xsl:for-eachselect="宝艾/*">
                        (<xsl:value-of select="position()"/>)<b><xsl:value-ofselect="."/></b><br/>
                </xsl:for-each>
        </xsl:template>
</xsl:stylesheet>
</xml>

<SCRIPT LANGUAGE="JavaScript">
<!--
var xslt = new ActiveXObject("Msxml2.XSLTemplate");
var xslDoc = newActiveXObject("Msxml2.FreeThreadedDOMDocument");
var xslProc;
xslDoc.async = false;
xslDoc.loadXML(xslsrc.xml);
xslt.stylesheet = xslDoc;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.async = false;
xmlDoc.loadXML(xmlsrc.xml);
xslProc = xslt.createProcessor();
xslProc.input = xmlDoc;
xslProc.transform();
document.write(xslProc.output);
//-->
</SCRIPT>[/html]

你可能感兴趣的:(MSXML2.FreeThreadedDOMDocument)