ASP调用XSLTemplate对象将XML转换为HTML示例

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>ASP调用XSLTemplate对象将XML转换为HTML示例-Mzwu.Com</title>
</head>

<body>
<%
'msxml版本
Const MsxmlVersion = ".3.0"

'通用函数
Function CreateAXObject(str)
    Set CreateAXObject = CreateObject(str)
End Function
Function CreateXMLDoc(str)
    Set CreateXmlDoc = CreateAXObject(str)
    CreateXMLDoc.Async = False
End Function
Function TransFormNode(strXML, strXSL)
    Set XMLDom = CreateXMLDoc("msxml2.FreeThreadedDOMDocument"& MsxmlVersion)
    Set XSLTemplate=CreateAXObject("Msxml2.XSLTemplate" & MsxmlVersion)
    Set XMLStyle=CreateXmlDoc("msxml2.FreeThreadedDOMDocument"& MsxmlVersion)
    
    On Error Resume Next
    If XMLDom.loadxml(strXML) Then
        If XMLStyle.loadxml(strXSL) Then
            XSLTemplate.stylesheet=XMLStyle
            Set proc = XSLTemplate.createProcessor()
            proc.input = XMLDom
            proc.transform()
            TransFormNode = proc.output
            Set proc=Nothing
        Else
            TransFormNode = "载入xsl错误..."
        End If
    Else
        TransFormNode = "载入xml错误..."
    End If
    If Err Then
        TransFormNode = "未知错误..."
        Err.Clear
    End If
    
    Set iXMLDom=Nothing
    Set XSLTemplate=Nothing
    Set XMLStyle=Nothing
End Function

'xml和xsl字符串
Dim strXML : strXML = "<?xml version=""1.0"" encoding=""gb2312""?><xml><website><name>木子屋</name><url>http://www.mzwu.com/</url></website><website><name>竹炭产品专卖</name><url>http://ztcp.taobao.com/</url></website></xml>"
Dim strXSL : strXSL = "<?xml version=""1.0"" encoding=""gb2312""?><xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""><xsl:template match=""/""><xsl:for-each select=""xml/website""><xsl:value-of select=""name"" />,<xsl:value-of select=""url"" /><br/></xsl:for-each></xsl:template></xsl:stylesheet>"

Response.Write TransFormNode(strXML, strXSL)
%>
</body>
</html>

代码参考自新云CMS4.0SP2

评论: 0 | 引用: 0 | 查看次数: 4285
发表评论
登录后再发表评论!