温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

XML实现BBS的案例

发布时间:2020-10-24 16:59:17 来源:亿速云 阅读:136 作者:小新 栏目:编程语言

小编给大家分享一下XML实现BBS的案例,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

表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 
  2-0-2,this is a test

  上面是BBS主题列表的一个例子。一般来说,假如不是使用Oracle(Oracle 有一条查询语句可以自动生成家族树,请查阅Select ... startwith ... 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"?>
  <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 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 & "<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.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是不同的

看完了这篇文章,相信你对XML实现BBS的案例有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI