星期二, 八月 31, 2004

如何在IE Javascript 中传递Node-seet到XSLT

目的: 在Javascript 利用Microsoft MXSML Engine来做XML/XSLT的ProtoTye开发.
传递一个XML的所谓.Result TRee Fragment 到XSLT 去处理.


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml06192000.asp

例子:
关键处:MSXSL的DoMDocument的属性 .documentElement把一个node-set直接传递到XSLT中去处理.
MSXML:4.0 sp2.可能是个Bug.


<SCRIPT language = "javascript">
function init()
{

var basicinfo = new ActiveXObject("Msxml2.DOMDocument.4.0");
var employment = new ActiveXObject("Msxml2.DOMDocument.4.0");
var paycatagory = new ActiveXObject("Msxml2.DOMDocument.4.0");
var attendence = new ActiveXObject("Msxml2.DOMDocument.4.0");
var mainpage = new ActiveXObject("Msxml2.DOMDocument.4.0");

mainpage.async=false;
mainpage.resolveExternals = false;

basicinfo.async=false;
employment.async=false;
paycatagory.async=false;
attendence.async=false;

// You can substitute other XML file names here.
try
{
mainpage.load("hello.xml");

basicinfo.load("emp_M_Basicinfo.xml");

employment.load("emp_M_EmploymentInfo.xml");

paycatagory.load("pay_M_CatagoryItem.xml");
attendence.load("attendence.xml");
}catch(e)
{
alert("error:"+e.parseError);
}
//Display basicinfo nodeset
// alert(basicinfo.documentElement.childNodes.item(0).xml);

var xslt = new ActiveXObject("Msxml2.XSLTemplate.4.0");
var xsltTree= new ActiveXObject("Msxml2.FreeThreadedDOMDocument.4.0");

xsltTree.async = false;
// You can substitute other XSLT file names here.
xsltTree.load("hello.xslt");
xslt.stylesheet = xsltTree

xslProc = xslt.createProcessor();

xslProc.input = mainpage
try
{
xslProc.addParameter("basicinfo",basicinfo.documentElement);//.selectNodes("/Results")
xslProc.addParameter("employment",employment);
xslProc.addParameter("paycatagory",paycatagory);
xslProc.addParameter("attendence",attendence);
xslProc.addParameter("yourname","William Wang");
}catch(e)
{
alert(e);
}
xslProc.transform();

resTree.innerHTML = xslProc.output;

// Save the result of the XSL transformation

//htmlCode = xslProc.output
//fso=new ActiveXObject("Scripting.FileSystemObject")
//htmlFile=fso.CreateTextFile("sample.html",true)
//htmlFile.WriteLine(htmlCode)
//htmlFile.Close()
}
</SCRIPT>
</HEAD>
<BODY onload = "init()" >
<div id="resTree"></div>
</BODY>
</HTML>