沐枫(初稿)
表A:
- 1-0-1,this is a test
- 3-1-1,this is a test
- 4-3-1,this is a test
- 5-3-1,this is a test
- 3-1-1,this is a test
- 2-0-2,this is a test
上面是BBS主题列表的一个例子。一般来说,假如不是使用ORACLE(Oracle 有一条查询语句可以自动生成家族树,请查阅select ... start with ... connect by ...语句),那么如何实现上例的列表是一件费事的工作(相信许多程序员都写过)。
如果我们改用XML来实现,那么结果会怎么样呢?
现在我们使用"Select * from bbs"从数据库中查询贴子,并以XML格式返回(如果你是用ADO,那么可以用其RecordSet.Save ... adPersistXML直接生成,当然如果你不喜欢ADO生成的格式,可用程序生成,如本例):
表B:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="b.xsl"?>
4-3-1,this is a test
slddfjslajfsdljf
5-3-1,this is a test
slddfjslajfsdljf
3-1-1,this is a test
slddfjslajfsdljf
1-0-1,this is a test
slddfjslajfsdljf
2-0-2,this is a test
slddfjslajfsdljf
说明:这里sid是贴子的id号,pid是贴子的父id号。title是标题,content是贴子的内容。
上表中第二行是指定使用b.XSL来转换XML内容。这是提供给IE5的信息。假如你使用XMLdom,那么可以不要这条信息。
我们再来看看将上表的XML内容显示成表A形式的XSL文件是怎么实现的:
表C: b.XSL
<?xml version='1.0'?>
http://www.w3.org/TR/WD-xsl">
现在,你将表B的内容存为abc.xml,将表C的内容存为b.xsl,然后在IE5中打开,你就可以看到和表A一样的内容了。
因此可以看出,XSL文件解定了最终的显示结果。假如你有多个子论坛,那么无需更改论坛程序,只要为各个子论坛提供不同XSL文件,就可以让各个子论坛的版而不论风格画面还是主题排列都会具有独特的表现。如果提供免费论坛服务,那么允许论坛申请者定制自已的XSL文件将是一个良好的选择。
但是假如客户端不支持XML,该怎么办呢?答案很简单,由服务端先将XML转换成HTML,再传到客户端。
下面我们以IIS4/5+IE5+ASP来实现这个例子(服务器必需安装IE5):
jscript %>
Set rsXML=Server.Createobject("ADOdb.RecordSet");
ssql = “SELECT * from bbs"
SCOnn = “你自个儿写”
rsXML.CursorLocation = adUseClient
rsXML.Open sSQL, sConn, adOpenStatic
//指定XSL文件位置
var styleFile = Server.MapPath("simple.xsl");
// Save the XML to XMLDOM
var source = Server.CreateObject("microsoft.XMLDOM");
' rsXML.Save source, adPersistXML
'我相当不喜欢ADO直接Save出来的XML文档,我总是这样做:
Dim GetData,v
GetData = GetData & ""
while not RS_ForumInfo.EOF
GetData = GetData & ""
for i = 0 to RS_ForumInfo.Fields.Count -1
set v = RS_ForumInfo.Fields.Item(i)
if (v.Type=201)or(v.Type=203)or(v.Type=205) then
GetData = GetData& "" &_
"" &_
"" & RS_ForumInfo.Fields.Item(i).Name &">"
else
GetData = GetData& "" &_
RS_ForumInfo.Fields.Item(i).Value &_
"" & RS_ForumInfo.Fields.Item(i).Name &">"
end if
set v = Nothing
next
GetData = GetData & ""
RS_ForumInfo.MoveNext
wend
GetData = GetData & ""
source.loadXML GetData
// Load the XSL
var style = Server.CreateObject("Microsoft.XMLDOM");
style.async = false;
style.load(styleFile);
Response.Write(source.transformNode(style));
%>
当然,由于此处为了简便,直接使用ADO来生成XML,因此simple.xsl和上面的b.xsl是不同的。
读者可以参考上例和XSL参考资料(2000年的MSDN有比较详细的XML/XSL SDK文档)来编写。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10748419/viewspace-1008827/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10748419/viewspace-1008827/