让Asp与XML交互

XML教程 2025-08-24

XML是标准扩展语言,是未来Web编程的标准,asp是现在广为流传的web编程语言之一,能不能让他们两个联合起来发挥作用呢?豆腐在这里给大家提供一个很简单的例子关于XML和XSL限于篇幅和知识水平豆腐就不在这里献丑了下面首先来说说几个需要用到的文件的内容

testXsl.xsl:


?xmlversion='1.0'?
xsl:stylesheetxmlns:xsl="http://www.***w3.org/TR/WD-xsl"
xsl:templatematch="/"
html
body
xsl:for-eachselect="PERSONNEL/PERSON"
xsl:choose
xsl:whenmatch=".[FG='boy']"
inputtype="text"
xsl:attributename="value"
xsl:value-ofselect="NAME"/
/xsl:attribute
/input
br/
/xsl:when
xsl:otherwisematch=".[FG='girl']"
fontcolor="red"lixsl:value-ofselect="NAME"//li/font
br/
/xsl:otherwise
xsl:otherwise
fontcolor="blue"xsl:value-ofselect="NAME"//font
/xsl:otherwise
/xsl:choose

/xsl:for-each
/body
/html
/xsl:template
/xsl:stylesheet


testXML.xml:
?xmlversion="1.0"encoding="gb2312"?
PERSONNEL
PERSON
NAME男性/NAME
FGboy/FG
/PERSON
PERSON
NAME女性/NAME
FGgirl/FG
/PERSON
PERSON
NAME呵呵,这个可不好说/NAME
FGdonotknow/FG
/PERSON
/PERSONNEL


testXML.asp

%
setxml=Server.CreateObject("Microsoft.XMLDOM")
xml.async=false
xml.load(server.mappath("testXML.xml"))

setxsl=Server.CreateObject("Microsoft.XMLDOM")
xsl.async=false
xsl.load(server.mappath("testXSL.xsl"))

Response.Write(xml.transformNode(xsl))

%

对照这个例子,我们主要来讲一下testXML.asp文件
setxml=Server.CreateObject("Microsoft.XMLDOM")
setxsl=Server.CreateObject("Microsoft.XMLDOM")
用来分别创建一个xml和xsl的实例,其中xml.load(server.mappath("testXML.xml"))用来加载
包含数据的xml文件,xsl.load(server.mappath("testXSL.xsl"))用来加载包含数据规则的xsl
文件,最终利用xml.transformNode(xsl)将前面的规则使用在XML文件中