使用XML实现BBS(主题列表篇)

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
his is a test
  3-1-1,this is a test
  4-3-1,this is a test
  5-3-1,this is a test
  2-0-2,this is a test
  上面是 BBS主题列表的一个例子。一般来说,假如不是使用Oracle(Oracle 有一条查询语句可以自动生成家族树,请查阅Select ... startwith ... connect by ...语句),那么如何 实现上例的列表是一件费事的工作(相信许多程序员都写过)。
  如果我们改用 XML实现,那么结果会怎么样呢?
  现在我们使用"Select * from BBS"从数据库中查询贴子,并以 XML格式返回(如果你是用ADO,那么可以用其RecordSet.Save ... adPersist XML直接生成,当然如果你不喜欢ADO生成的格式,可用程序生成,如本例):
  表B:
  <? XML version="1.0"?>
  <? XML-stylesheet type="text/xsl" href="b.xsl"?>
  < BBS>
  <post sid="4" pid="3" aid="1">
  <title>4-3-1,this is a test</title>
  <content>slddfjslajfsdljf</content>
  </post>
  <post sid="5" pid="3" aid="1">
  <title>5-3-1,this is a test</title>
  <content>slddfjslajfsdljf</content>
  </post>
  <post sid="3" pid="1" aid="1">
  <title>3-1-1,this is a test</title>
  <content>slddfjslajfsdljf</content>
  </post>
  <post sid="1" pid="0" aid="1">
  <title>1-0-1,this is a test</title>
  <content>slddfjslajfsdljf</content>
  </post>
  <post sid="2" pid="0" aid="2">
  <title>2-0-2,this is a test</title>
  <content>slddfjslajfsdljf</content>
  </post>
  </ BBS>
  说明:这里sid是贴子的id号,pid是贴子的父id号。title是标题,content是贴子的内容。
  上表中第二行是指定使用b.XSL来转换 XML内容。这是提供给IE5的信息。假如你使用 XMLDOM,那么可以不要这条信息。
  我们再来看看将上表的 XML内容显示成表A形式的XSL文件是怎么 实现的:
  表C:b.XSL
  <? XML version=''1.0''?>
  <xsl:stylesheet XMLns:xsl="http://www.w3.org/TR/WD-xsl">
  <xsl:template match="/">
  <html>
  <body>
   <xsl:apply-templates select="*"/>
  </body>
  </html>
  </xsl:template>
  <xsl:template match="post">
  <li>
   <div>
   <xsl:attribute name="title"><xsl:value-of select="content"/></xsl:attribute>
   <xsl:value-of select="title"/>
   <xsl:if test="/ BBS/post[@pid=context()/@sid]">
   <xsl:element name="ul">
   <xsl:apply-templates select="/ BBS/post[@pid=context()/@sid]"/>
   </xsl:element>
   </xsl:if>
   </div>
  </li>
  </xsl:template>
  <xsl:template match=" BBS">
  <ul>
  <xsl:apply-templates select="post[@pid=0]"/>
  </ul>
  </xsl:template>
  </xsl:stylesheet>
  现在,你将表B的内容存为abc. XML,将表C的内容存为b.xsl,然后在IE5中打开,你就可以看到和表A一样的内容了。
  因此可以看出,XSL文件解定了最终的显示结果。假如你有多个子论坛,那么无需更改论坛程序,只要为各个子论坛提供不同XSL文件,就可以让各个子论坛的版而不论风格画面还是主题排列都会具有独特的表现。如果提供免费论坛服务,那么允许论坛申请者定制自已的XSL文件将是一个良好的选择。
  但是假如客户端不支持 XML,该怎么办呢?答案很简单,由服务端先将 XML转换成HTML,再传到客户端。
  下面我们以IIS4/5 IE5 ASP来 实现这个例子(服务器必需安装IE5):
  <%@ LANGUAGE = JScript %>
  <%
  Set rs XML=Server.CreateObject("ADODB.RecordSet");
  sSQL = “SELECT * from BBS"
  sConn = “你自个儿写”
  rs XML.CursorLocation = adUseClient
  rs XML.Open sSQL, sConn, adOpenStatic
  //指定XSL文件位置
  var styleFile = Server.MapPath("simple.xsl");
  // Save the XML to XMLDOM
  var source = Server.CreateObject("Microsoft. XMLDOM");
  ''rs XML.Save source, adPersist XML
  ''我相当不喜欢ADO直接Save出来的 XML文档,我总是这样做:
  Dim GetData,v
  GetData = GetData & "< BBS>"
  while not RS_ForumInfo.EOF
  GetData = GetData & "<post>"
  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 &">" &_
  "<![CDATA[" & RS_ForumInfo.Fields.Item(i).Value & "]]>" &_
  "</" & RS_ForumInfo.Fields.Item(i).Name &">"
  else
  GetData = GetData& "<" & RS_ForumInfo.Fields.Item(i).Name &">" &_
  RS_ForumInfo.Fields.Item(i).Value &_
  "</" & RS_ForumInfo.Fields.Item(i).Name &">"
  end if
  set v = Nothing
  next
  GetData = GetData & "</post>"
  RS_ForumInfo.MoveNext
  wend
  GetData = GetData & "</ BBS>"
  source.load XML 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文档)来编写。(完) <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值