xslt 转换方法

一、客户端转换

    function XsltTransform(xslfile)
  
       {        
  
           if(typeof(window.ActiveXObject) != 'undefined')
  
           {
  
               //ie
 
              try
 
              {
 
                  var xmlDoc=new ActiveXObject("Msxml2.DOMDocument.3.0");
 
                  var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
 
                  xmlDoc.async=false;
 
                  xslDoc.async = false;
 
                 xmlDoc.loadXML(document.getElementById("xmlContent").value.replace(/\r\n/gi,""));
 
                 xslDoc.load(xslfile);
 
                
 
                 var oTemplate = new ActiveXObject("Msxml2.XSLTemplate");
 
                 oTemplate.stylesheet = xslDoc;
 
                 var oProcessor = oTemplate.createProcessor();
 
                  oProcessor.input = xmlDoc.documentElement;
 
                                 
 
                  oProcessor.addParameter("parameter",'<%=count%>');
 
                  oProcessor.transform();
 
                 
 
                  document.getElementById('div').innerHTML=oProcessor.output;
 
                 
 
              }catch(e){
 
                
 
              }
 
          }   
 
          else if(document.implementation && document.implementation.createDocument)
 
          { 
 
              //ff
 
              try
 
              {
 
                  var parser=new DOMParser();
 
                  var xmlDoc;
 
                  if(document.getElementById("xmlContent").value == "")                
 
                     xmlDoc = parser.parseFromString(text,"text/xml");
 
                  else
 
                      xmlDoc = parser.parseFromString(document.getElementById("xmlContent").value.replace(/\r\n/gi,""),"text/xml");
 
                 
 
                  xmlDoc.async = false;
 
                  
 
                  var xslDoc = document.implementation.createDocument("", "", null);
 
                  xslDoc.async = false; 
 
                  xslDoc.load(xslfile);    
 
 
 
                // 定义XSLTProcessor对象
 
                  var xsltProcessor = new XSLTProcessor();
 
                  xsltProcessor.importStylesheet(xslDoc);
 
                  xsltProcessor.setParameter(null, "parameter", '<%=count %>');
 
                 // transformToDocument方式
 
                 var result = xsltProcessor.transformToDocument(xmlDoc);
 
                 document.getElementById('div').innerHTML = result.documentElement.textContent;
 
             }catch(e){   
 
             }
 
         }
 
     }
 

二、服务器端方法:

 XslTransform xslt = new XslTransform();     
xslt.Load(
"print_root.xsl");
XmlDocument doc 
= new XmlDocument();
doc.Load(
"library.xml");
// Create a new document containing just the result tree fragment.
XmlNode testNode = doc.DocumentElement.FirstChild; 
XmlDocument tmpDoc 
= new XmlDocument(); 
tmpDoc.LoadXml(testNode.OuterXml);
// Pass the document containing the result tree fragment 
// to the Transform method.
Console.WriteLine("Passing " + tmpDoc.OuterXml + " to print_root.xsl");
xslt.Transform(tmpDoc, 
null, Console.Out, null);

posted @ 2008-07-21 09:36  老大卫  阅读(229)  评论(0编辑  收藏  举报