ASP实现XML+XSL生成HTML

 搞了半天,终于生成功了

贴代码

test.asp

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
cHtmLFile = "test.htm"
cXMLFile =server.mappath("test.xml")
cXSLFile = Server.MapPath("test.xslt")
cOutputFile = Server.MapPath(cHtmLFile)
Set oXML = Server.CreateObject("Microsoft.XMLDOM")
oXML.async = false
oXML.ValidateOnParse=False
oXML.load(cXMLFile)
Set oXSL = Server.CreateObject("Microsoft.XMLDOM")
oXSL.async = false
oXSL.ValidateOnParse=False
oXSL.load(cXSLFile)
Set oOutput = Server.CreateObject("Microsoft.XMLDOM")
oOutput.async = false
oOutput.ValidateOnParse=False
set objAdoStream = Server.CreateObject("ADODB.Stream")
objAdoStream.Type = 1
objAdoStream.Open()
oXML.transformNodeToObject oXSL, objAdoStream
objAdoStream.SaveToFile cOutputFile,2
objAdoStream.Close()
%>

test.xml

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xslt"?>
<musicCollection title="test" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
<album title="Beyond Reality" artist="Dreamtale" year="2002">
<soundTrack tracknumber="01" artist="Dreamtale" year="2002" title="The Dawn" lenth="246282" />
<soundTrack tracknumber="02" artist="Dreamtale" year="2002" title="Memories of Time" lenth="286902" />
<soundTrack tracknumber="03" artist="Dreamtale" year="2002" title="Refuge From Reality" lenth="285857" />
<soundTrack tracknumber="04" artist="Dreamtale" year="2002" title="Fallen Star" lenth="265613" />
<soundTrack tracknumber="05" artist="Dreamtale" year="2002" title="Heart&apos;s Desire" lenth="490553" />
<soundTrack tracknumber="06" artist="Dreamtale" year="2002" title="Time of Fatherhood" lenth="325720" />
<soundTrack tracknumber="07" artist="Dreamtale" year="2002" title="Where The Rainbow Ends" lenth="301635" />
<soundTrack tracknumber="08" artist="Dreamtale" year="2002" title="Dreamland" lenth="218331" />
<soundTrack tracknumber="09" artist="Dreamtale" year="2002" title="Call Of The Wild" lenth="323552" />
<soundTrack tracknumber="10" artist="Dreamtale" year="2002" title="Secret Wish" lenth="323631" />
<soundTrack tracknumber="11" artist="Dreamtale" year="2002" title="Silent Path" lenth="292754" />
<soundTrack tracknumber="12" artist="Dreamtale" year="2002" title="Farewell..." lenth="435879" />
</album>
<album title="Up Against the Wall" artist="Madcat &amp; Kane" year="1999">
<soundTrack tracknumber="01" artist="Madcat &amp; Kane" year="1999" title="Moon Goin&apos; Down" lenth="239255" />
<soundTrack tracknumber="02" artist="Madcat &amp; Kane" year="1999" title="Act Like You Love Me" lenth="205270" />
<soundTrack tracknumber="03" artist="Madcat &amp; Kane" year="1999" title="Stones in My Passway" lenth="203363" />
<soundTrack tracknumber="04" artist="Madcat &amp; Kane" year="1999" title="You Used to Call Me Mama" lenth="201534" />
<soundTrack tracknumber="05" artist="Madcat &amp; Kane" year="1999" title="I&apos;m Doing All Right" lenth="279118" />
<soundTrack tracknumber="06" artist="Madcat &amp; Kane" year="1999" title="Darkness" lenth="238497" />
<soundTrack tracknumber="07" artist="Madcat &amp; Kane" year="1999" title="Did You Save Me a Taste?" lenth="220525" />
<soundTrack tracknumber="08" artist="Madcat &amp; Kane" year="1999" title="Rosalie" lenth="500950" />
<soundTrack tracknumber="09" artist="Madcat &amp; Kane" year="1999" title="Black Cat Bone" lenth="254458" />
<soundTrack tracknumber="10" artist="Madcat &amp; Kane" year="1999" title="You Got to Move" lenth="307879" />
<soundTrack tracknumber="11" artist="Madcat &amp; Kane" year="1999" title="Angel from Montgomery" lenth="360724" />
<soundTrack tracknumber="12" artist="Madcat &amp; Kane" year="1999" title="I Believe in a New Day" lenth="301688" />
<soundTrack tracknumber="13" artist="Madcat &amp; Kane" year="1999" title="C. C. Rider" lenth="359183" />
</album>
</musicCollection>

test.xslt

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp "&#160;">
<!ENTITY copy "&#169;">
<!ENTITY reg "&#174;">
<!ENTITY trade "&#8482;">
<!ENTITY mdash "&#8212;">
<!ENTITY ldquo "&#8220;">
<!ENTITY rdquo "&#8221;">
<!ENTITY pound "&#163;">
<!ENTITY yen "&#165;">
<!ENTITY euro "&#8364;">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />
<xsl:template match="/">
<html>
<head>
<title>Test</title>
<link href="test.css" rel="stylesheet" type="text/css" />
</head>
<body>
<xsl:for-each select="musicCollection">
<xsl:for-each select="album">
<br />
<table border="1" cellspacing="0">
<caption>
专辑名:<xsl:value-of select="@title" /> 艺术家:<xsl:value-of select="@artist" />
</caption>
<thead>
<tr>
<th>音轨号</th>
<th>歌曲名</th>
<th>持续时间</th>
</tr>
</thead>
<tbody>
<xsl:for-each select="soundTrack">
<tr>
<td><xsl:value-of select="@tracknumber" /></td>
<td><xsl:value-of select="@title" /></td>
<td><xsl:value-of select="floor(@lenth div 60000)" />"<xsl:value-of select="format-number(floor(@lenth div 1000) mod 60,'00')" />'</td>
</tr>
</xsl:for-each>
</tbody>
<tfoot>
<tr>
<th colspan="3">共<xsl:value-of select="count(soundTrack)" />首曲目 总时长<xsl:value-of select="floor(sum(soundTrack/@lenth) div 3600000)" />:<xsl:value-of select="format-number(floor(sum(soundTrack/@lenth) div 60000) mod 60,'00')" />"<xsl:value-of select="format-number(floor(sum(soundTrack/@lenth) div 1000) mod 60,'00')" />'</th>
</tr>
</tfoot>
</table>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Time limit is exhausted. Please reload CAPTCHA.