Home  >  Article  >  Backend Development  >  Detailed introduction to using XML to implement BBS (topic list)

Detailed introduction to using XML to implement BBS (topic list)

黄舟
黄舟Original
2017-03-04 17:12:331663browse

Table 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

The above is an example of a BBS topic list. Generally speaking, if you are not using Oracle (Oracle has a query statement that can automatically generate a family tree, please refer to the Select... startwith... connect by... statement), then how to implement the list in the above example is troublesome. Work (I believe many programmers have written this).
If we use xml instead, what will be the result?
Now we use "Select * from bbs" to query posts from the database and return them in XML format (if you are using ADO, you can use its RecordSet.Save... adPersistXML to generate it directly. Of course, if you don't like it, The format generated by ADO can be generated by a program, such as this example):
Table 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>

Description: Here sid is the id number of the post, and pid is the parent id number of the post. title is the title and content is the content of the post.
The second row in the above table specifies the use of b.XSL to convert XML content. This is the information provided to IE5. If you use XMLDOM, you don't need this message.
Let’s take a look at how to display the XML content of the above table into an XSL file in the form of Table A:
Table C: b.XSL

<?xml version=&#39;&#39;1.0&#39;&#39;?>
  <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>

Now, you will table Save the content of B as abc.xml, save the content of table C as b.xsl, and then open it in IE5, you can see the same content as table A.
It can be seen that the XSL file determines the final display result. If you have multiple sub-forums, there is no need to change the forum program. As long as you provide different XSL files for each sub-forum, you can make each sub-forum have a unique performance regardless of the style, screen or theme arrangement. If free forum services are provided, it would be a good choice to allow forum applicants to customize their own XSL files.
But what should we do if the client does not support XML? The answer is simple. The server first converts XML into HTML and then transmits it to the client.
Below we use IIS4/5+IE5+asp to implement this example (the server must have IE5 installed):

<%@ 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");
  &#39;&#39;rsXML.Save source, adPersistXML
  &#39;&#39;我相当不喜欢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));
  %>

Of course, for the sake of simplicity, ADO is used directly to generate XML, so simple.xsl is different from b.xsl above.
Readers can refer to the above example and XSL reference materials (MSDN in 2000 has a more detailed XML/XSL SDK document) to write. (End)

The above is the detailed introduction of using XML to implement BBS (topic list). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn