使用ASP處理XSLT轉換XML比較簡單,思路如下:建立一個XSLTemplate的對象,再建立一個XMLDOM對象,然後在家Xml檔案和XSLT檔案,最後使用方法transform即可,包含到類裡面,具體代碼如下:
以下是引用片段:
Class Cls_Xml_Transform
Private lInput,XSLTemplate
Private p_Output
Public Property Get Output()
Output = p_Output
End Property
Private Property Let Output(ByVal strInfo)
p_Output = strInfo
End Property
Public Property Let Input(ByVal vNewValue)
If IsObject(vNewValue) Then Set lInput=vNewValue
End Property
Public Property Let XSLTemplatefile(ByVal vNewValue)
Dim StyleSheet
Dim vNewValue_
vNewValue_ = vNewValue
If Not InStr(vNewValue,":\") > 0 Then
vNewValue = Server.MapPath(vNewValue)
End If
Set XSLTemplate=Server.CreateObject("Msxml2.XSLTemplate")
Set StyleSheet=Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
StyleSheet.load vNewValue
XSLTemplate.StyleSheet=StyleSheet
End Property
Public Sub Transform()
Dim proc
Set proc = XSLTemplate.createProcessor()
proc.input=linput
proc.transform()
Output=proc.output
Set proc=Nothing
End Sub
End Class
使用範例:
以下是引用片段:
Set XMLDOM = Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
XMLDOM.async = false
XMLDOM.load(Server.MapPath("bi2.xml"))
Set o=new Cls_IO_Transform
o.XSLTemplatefile="bi2.xsl"
o.Input=XMLDOM
o.Transform()
response.write o.Output()
這裡處理的直接是XmlDom對象了,如果需要,靈活可以修改。