asp取动态表单中数据并写入xml文件,用xsl显示
程序员文章站
2022-05-03 12:02:14
<html>
<head>
<title>无标题文档</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<script language="javascript">
var currow=null;
var y=1;
function selectrow(){
var e=window.event;
var tr1=e.srcelement;
if(currow)
currow.bgcolor="#ffffff";
tr1.bgcolor="e7e7e7";
currow=tr1;
}
function addrow(src){
//alert(src);
var newrow = src.insertrow(src.rows.length-1);
newrow.attachevent("onclick",selectrow);
newrow.height=20;
var i=4;
while(i--){
var newcell = newrow.insertcell();
switch(i){
case 0: newcell.innerhtml= '<input type="button" onclick="javascript:delrow(this.parentelement.parentelement)" value="删除此行">'+(y++);
document.all.count.value=y-1;
break;
case 1:newcell.innerhtml=div2.innerhtml;break;
case 2:newcell.innerhtml=div3.innerhtml;break;
case 3:newcell.innerhtml=div4.innerhtml;break;
default: newcell.innerhtml=div1.innerhtml;break;
}
}
//alert(newrow.outerhtml);
}
function delrow(src){
var tab=src.parentelement;
var i=tab.rows.length;
var j=tab.rows.length;
while(i--){
if(src==tab.rows[i]){
//alert("就是这行----"+ i);
document.all.count.value=j-3;
tab.deleterow(i);
}
}
}
</script>
</head>
<body>
<form name="form1" action="2.asp" method="post" onsubmit=return(checkv())>
<h3>请输入经销商的通讯信息:</h3>
姓 名: <input type="text" name="jxname"><br>
地 址: <input type="text" name="jxadd"><br>
电 话: <input type="text" name="jxtel"><br>
请输入库存信息<br>
<table id="tb" width="100%" border="1" align="center" cellpadding="1" cellspacing="1" style="border-collapse:collapse" bordercolor="#111111">
<tr>
<th scope="col" width="25%">品名</th>
<th scope="col" width="25%">规格</th>
<th scope="col" width="25%">库存量</th>
<th scope="col" width="25%">操作</th>
</tr>
<tr id="blankrow" onclick="addrow(this.parentelement)">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<input type="hidden" name="count" value="count">
<input type="submit" id="btnsub" name="btnsub" value="提交"><br>
</form>
<div id="div1" style="display:none "><input id="txt" type="text" name="id" style="width:97%; background-color:#ffffef"></div>
<div id="div2" style="display:none "><input id="txt" type="text" name="id1" style="width:97%; background-color:#ffffef" value="11"></div>
</body>
<div id="div3" style="display:none "><select name="id2" style="width:97%; background-color:#ffffef" >
<option value="箱">箱</option>
<option value="件">件</option>
</select></div>
<div id="div4" style="display:none "><input id="txt" type="text" name="id3" style="width:97%; background-color:#ffffef" value="33"></div>
<script language="javascript">
function checkv()
{
if (form1.jxname.value.length == 0)
{
alert("请填写姓名和地址!");
form1.jxname.focus();
return false;
}
else if(!(check_number(form1.id1.value)))
{
alert("数量只能为数字")
form1.id1.focus();
return false;
}
else
return (true);
}
function check_number(myint)
{
var checkok = "0123456789";
var checkstr = myint;
var allvalid = true;
var decpoints = 0;
var allnum = "";
for (i = 0; i < checkstr.length; i++)
{
ch = checkstr.charat(i);
for (j = 0; j < checkok.length; j++)
if (ch == checkok.charat(j))
break;
if (j == checkok.length)
{
allvalid = false;
break;
}
allnum += ch;
}
if (!allvalid)
{
return (false);
}
return (true);
}
</script>
</html>
2. 2.asp文件
<%
'////////////////////取表单数据
response.write("共"+request.form("count")+"记录")
i=request.form("count")
jxname=request.form("jxname")
jxadd=request.form("jxadd")
jxtel=request.form("jxtel")
response.write(request.form("jxname"))
response.write("<br>")
response.write(request.form("jxadd"))
response.write("<br>")
response.write(request.form("jxtel"))
response.write("<br>")
str1=trim(request.form("id1"))
str2=trim(request.form("id2"))
str3=trim(request.form("id3"))
arraya=split(str1,",")
arrayb=split(str2,",")
arrayc=split(str3,",")
for i = 0 to ubound(arraya)
response.write "<li>" & arrayc(i)&"--"&arrayb(i)&"--"&arraya(i)
next
'///////////////////写入xml文件
dim objdom
dim objroot
dim objchild1
dim objchild2
dim objchild3
dim objchild4
dim objpi
dim pinode
set objdom = server.createobject("microsoft.xmldom")
'/////写入样式
set pinode=objdom.createprocessinginstruction("xml-stylesheet", "type='text/xsl'
href='list.xsl'")
objdom.appendchild pinode
'/////创建根接点
set objroot = objdom.createelement("kucunbiao")
objdom.appendchild objroot
set objchild1 = objdom.createelement("jxname")
objchild1.text=jxname
objroot.appendchild objchild1
set objchild2 = objdom.createelement("jxadd")
objchild2.text=jxadd
objroot.appendchild objchild2
set objchild3=objdom.createelement("jxtel")
objchild3.text=jxtel
objroot.appendchild objchild3
set objchild4=objdom.createelement("kucun")
objroot.appendchild objchild4
for i = 0 to ubound(arraya)
set objfield = objdom.createelement("field")
'创建属性taborder。
set objatttaborder = objdom.createattribute("taborder")
'设定taborder的属性值
objatttaborder.text = i
'把taborder的属性值追加到field元素中去。
objfield.setattributenode objatttaborder
'创建一个新的元素field_value.
set objfieldvalue2= objdom.createelement("name")
objfieldvalue2.text = arrayc(i)
set objfieldvalue = objdom.createelement("guige")
objfieldvalue.text = arrayb(i)
set objfieldvalue1=objdom.createelement("shuliang")
objfieldvalue1.text = arraya(i)
'追加field元素为根元素的子元素。
objchild4.appendchild objfield
'追加field_value做为子元素的内容
objfield.appendchild objfieldvalue
objfield.appendchild objfieldvalue1
objfield.appendchild objfieldvalue2
next
set objpi = objdom.createprocessinginstruction("xml","version='1.0'")
objdom.insertbefore objpi, objdom.childnodes(0)
objdom.save "f:\xml\"&jxname&".xml"
'//////////释放资源
set objdom = nothing
set objroot = nothing
set objfield = nothing
set objfieldvalue = nothing
set objattid = nothing
set objatttaborder = nothing
set objpi = nothing
set pinode=nothing
%>
3. 用xsl显示提交的xml,list.xsl
<?xml version="1.0" encoding="gb2312" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">
<xsl:template match="/">
<html>
<body style="background:#dfeebb"><left>
<h3>姓名:<xsl:value-of select="kucunbiao/jxname"/></h3>
<h3>地址:<xsl:value-of select="kucunbiao/jxadd"/></h3>
<h3>电话:<xsl:value-of select="kucunbiao/jxtel"/></h3>
<table border="1" width="600">
<xsl:apply-templates select="kucunbiao/kucun" >
</xsl:apply-templates>
</table></left>
</body>
</html>
</xsl:template>
<xsl:template match="kucun">
<tr>
<td width="60%"><div align="center">名称</div></td>
<td width="20%"><div align="center">规格</div></td>
<td width="20%"><div align="center">数量</div></td>
</tr>
<xsl:for-each select="field">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="guige"/></td>
<td><xsl:value-of select="shuliang"/></td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<head>
<title>无标题文档</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<script language="javascript">
var currow=null;
var y=1;
function selectrow(){
var e=window.event;
var tr1=e.srcelement;
if(currow)
currow.bgcolor="#ffffff";
tr1.bgcolor="e7e7e7";
currow=tr1;
}
function addrow(src){
//alert(src);
var newrow = src.insertrow(src.rows.length-1);
newrow.attachevent("onclick",selectrow);
newrow.height=20;
var i=4;
while(i--){
var newcell = newrow.insertcell();
switch(i){
case 0: newcell.innerhtml= '<input type="button" onclick="javascript:delrow(this.parentelement.parentelement)" value="删除此行">'+(y++);
document.all.count.value=y-1;
break;
case 1:newcell.innerhtml=div2.innerhtml;break;
case 2:newcell.innerhtml=div3.innerhtml;break;
case 3:newcell.innerhtml=div4.innerhtml;break;
default: newcell.innerhtml=div1.innerhtml;break;
}
}
//alert(newrow.outerhtml);
}
function delrow(src){
var tab=src.parentelement;
var i=tab.rows.length;
var j=tab.rows.length;
while(i--){
if(src==tab.rows[i]){
//alert("就是这行----"+ i);
document.all.count.value=j-3;
tab.deleterow(i);
}
}
}
</script>
</head>
<body>
<form name="form1" action="2.asp" method="post" onsubmit=return(checkv())>
<h3>请输入经销商的通讯信息:</h3>
姓 名: <input type="text" name="jxname"><br>
地 址: <input type="text" name="jxadd"><br>
电 话: <input type="text" name="jxtel"><br>
请输入库存信息<br>
<table id="tb" width="100%" border="1" align="center" cellpadding="1" cellspacing="1" style="border-collapse:collapse" bordercolor="#111111">
<tr>
<th scope="col" width="25%">品名</th>
<th scope="col" width="25%">规格</th>
<th scope="col" width="25%">库存量</th>
<th scope="col" width="25%">操作</th>
</tr>
<tr id="blankrow" onclick="addrow(this.parentelement)">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<input type="hidden" name="count" value="count">
<input type="submit" id="btnsub" name="btnsub" value="提交"><br>
</form>
<div id="div1" style="display:none "><input id="txt" type="text" name="id" style="width:97%; background-color:#ffffef"></div>
<div id="div2" style="display:none "><input id="txt" type="text" name="id1" style="width:97%; background-color:#ffffef" value="11"></div>
</body>
<div id="div3" style="display:none "><select name="id2" style="width:97%; background-color:#ffffef" >
<option value="箱">箱</option>
<option value="件">件</option>
</select></div>
<div id="div4" style="display:none "><input id="txt" type="text" name="id3" style="width:97%; background-color:#ffffef" value="33"></div>
<script language="javascript">
function checkv()
{
if (form1.jxname.value.length == 0)
{
alert("请填写姓名和地址!");
form1.jxname.focus();
return false;
}
else if(!(check_number(form1.id1.value)))
{
alert("数量只能为数字")
form1.id1.focus();
return false;
}
else
return (true);
}
function check_number(myint)
{
var checkok = "0123456789";
var checkstr = myint;
var allvalid = true;
var decpoints = 0;
var allnum = "";
for (i = 0; i < checkstr.length; i++)
{
ch = checkstr.charat(i);
for (j = 0; j < checkok.length; j++)
if (ch == checkok.charat(j))
break;
if (j == checkok.length)
{
allvalid = false;
break;
}
allnum += ch;
}
if (!allvalid)
{
return (false);
}
return (true);
}
</script>
</html>
2. 2.asp文件
<%
'////////////////////取表单数据
response.write("共"+request.form("count")+"记录")
i=request.form("count")
jxname=request.form("jxname")
jxadd=request.form("jxadd")
jxtel=request.form("jxtel")
response.write(request.form("jxname"))
response.write("<br>")
response.write(request.form("jxadd"))
response.write("<br>")
response.write(request.form("jxtel"))
response.write("<br>")
str1=trim(request.form("id1"))
str2=trim(request.form("id2"))
str3=trim(request.form("id3"))
arraya=split(str1,",")
arrayb=split(str2,",")
arrayc=split(str3,",")
for i = 0 to ubound(arraya)
response.write "<li>" & arrayc(i)&"--"&arrayb(i)&"--"&arraya(i)
next
'///////////////////写入xml文件
dim objdom
dim objroot
dim objchild1
dim objchild2
dim objchild3
dim objchild4
dim objpi
dim pinode
set objdom = server.createobject("microsoft.xmldom")
'/////写入样式
set pinode=objdom.createprocessinginstruction("xml-stylesheet", "type='text/xsl'
href='list.xsl'")
objdom.appendchild pinode
'/////创建根接点
set objroot = objdom.createelement("kucunbiao")
objdom.appendchild objroot
set objchild1 = objdom.createelement("jxname")
objchild1.text=jxname
objroot.appendchild objchild1
set objchild2 = objdom.createelement("jxadd")
objchild2.text=jxadd
objroot.appendchild objchild2
set objchild3=objdom.createelement("jxtel")
objchild3.text=jxtel
objroot.appendchild objchild3
set objchild4=objdom.createelement("kucun")
objroot.appendchild objchild4
for i = 0 to ubound(arraya)
set objfield = objdom.createelement("field")
'创建属性taborder。
set objatttaborder = objdom.createattribute("taborder")
'设定taborder的属性值
objatttaborder.text = i
'把taborder的属性值追加到field元素中去。
objfield.setattributenode objatttaborder
'创建一个新的元素field_value.
set objfieldvalue2= objdom.createelement("name")
objfieldvalue2.text = arrayc(i)
set objfieldvalue = objdom.createelement("guige")
objfieldvalue.text = arrayb(i)
set objfieldvalue1=objdom.createelement("shuliang")
objfieldvalue1.text = arraya(i)
'追加field元素为根元素的子元素。
objchild4.appendchild objfield
'追加field_value做为子元素的内容
objfield.appendchild objfieldvalue
objfield.appendchild objfieldvalue1
objfield.appendchild objfieldvalue2
next
set objpi = objdom.createprocessinginstruction("xml","version='1.0'")
objdom.insertbefore objpi, objdom.childnodes(0)
objdom.save "f:\xml\"&jxname&".xml"
'//////////释放资源
set objdom = nothing
set objroot = nothing
set objfield = nothing
set objfieldvalue = nothing
set objattid = nothing
set objatttaborder = nothing
set objpi = nothing
set pinode=nothing
%>
3. 用xsl显示提交的xml,list.xsl
<?xml version="1.0" encoding="gb2312" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">
<xsl:template match="/">
<html>
<body style="background:#dfeebb"><left>
<h3>姓名:<xsl:value-of select="kucunbiao/jxname"/></h3>
<h3>地址:<xsl:value-of select="kucunbiao/jxadd"/></h3>
<h3>电话:<xsl:value-of select="kucunbiao/jxtel"/></h3>
<table border="1" width="600">
<xsl:apply-templates select="kucunbiao/kucun" >
</xsl:apply-templates>
</table></left>
</body>
</html>
</xsl:template>
<xsl:template match="kucun">
<tr>
<td width="60%"><div align="center">名称</div></td>
<td width="20%"><div align="center">规格</div></td>
<td width="20%"><div align="center">数量</div></td>
</tr>
<xsl:for-each select="field">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="guige"/></td>
<td><xsl:value-of select="shuliang"/></td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
上一篇: asp如何用discuz会员系统
下一篇: ASP使用MYSQL数据库全攻略