表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是不同的。
  读者可以参考上例和XSL参考资料(2000年的MSDN有比较详细的XML/XSL SDK文档)来编写。(完) 
 以上就是使用XML实现BBS(主题列表篇)的详情介绍的内容,更多相关内容请关注PHP中文网(www.php.cn)!
 
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
 
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号