Parsing the XML page of the CSDN forum (i)

Source: Internet
Author: User
Tags format cdata html page html tags version xmlns xsl xsl file
xml| page xml+xsl is also in recent years, we began to pay attention to the data and expression of separation of the way, in foreign countries have a good application, but in the domestic potential application of the scope is not very wide. As far as I can see, the first such application is of course the CSDN forum posts display page. Perhaps most of my friends, like me, saw the first time that the page with the end of the. xml suffix was more curious, when viewing the source of the page was just a label containing the data. Of course now these are no longer magical, as we all know that this is the role of XSL.
Why use XML to save data? Isn't the database better? Oh, our own small page estimated no more than 100 people at the same time browsing, but CSDN is not the same AH. No programmer does not know csdn, tens of thousands of people to visit, his database is too bad to estimate the AH. Using XML is actually a way to provide a class static page, because we go to open its page, the link address is an XML file, the browser will first down the XML file, and then according to the file provided in the XSL address to find the XSL and then parse the resulting page to show us. It's a joke on the csdn that it's for most diving enthusiasts, and it's not unreasonable.
Many people want to know how XSL constructs such a wonderful thing, but it's not difficult. You can just master some of the features of XSLT and some knowledge of HTML. Do you know some msxmldom knowledge? That's even better, let's take a step-by-step look at the CSDN page. Don't you know a lot? It does not matter, this is a beginner to learn the good Dongdong Oh, only the most basic grammatical structure can be built up. You know <xsl: value-of select= "..."/>, you also know Xsl:template and Xsl:call-template bar, OK, enough. Let's Go
Put the CSDN XML page into practice!
CSDN's forum page I divided it into three parts, problem topics, reply display and submit reply. The first two parts are parsed in XSLT, so what's the third part? Oh, is not the focus of our discussion. Let's take a look at the case of an XML file that csdn the content of the posts:
<?xml version= "1.0" encoding= "GB2312"?>
<?xml-stylesheet type= ' text/xsl ' href= ' topic.xsl '?>
<Topic>
<Issue>
<PostUserNickName> with Wind </PostUserNickName>
<rank>1</rank>
<ranknum>user1</ranknum>
<credit>100</credit>
<TopicId>3420145</TopicId>
<TopicName> waiting FOR XML problem online (server receive) </TopicName>
<PostUserId>449974</PostUserId>
<PostUserName>mazying99</PostUserName>
<roomname>web Service xml/soap</roomname>
<ReplyNum>2</ReplyNum>
<postdatetime>2004-9-29 23:55:18</postdatetime>
<Point>50</Point>
<ReadNum>6</ReadNum>
<RoomId>306</RoomId>
<EndState>0</EndState>
<content><! [cdata[...]] ></Content>
</Issue>
<Replys>
<Reply>
<PostUserNickName> sit in line 80,000 </PostUserNickName>
<rank>1</rank>
<ranknum>user1</ranknum>
<credit>100</credit>
<ReplyID>24484920</ReplyID>
<TopicID>3420145</TopicID>
<PostUserId>40078</PostUserId>
<PostUserName>guiqing</PostUserName>
<Point>0</Point>
<content><! [cdata[...]] ></Content>
<postdatetime>2004-9-30 7:52:03</postdatetime>
</Reply>
</Replys>
</Topic>
Of course I omitted the topic and reply, and you can see that topic is the master node of the entire XML file, with two children under it: Issue and Replys.
OK, it is obvious that issue store is the question and the author as well as the time and other related information; Replys stored a reply.
First of all, we analyze the issue node, most nodes do not need to explain all at a glance. Here's what you need to be aware of <rannum/> nodes. User1 's user refers to the lower grade is the triangle symbol (number marked a few symbols), if the high level is star1 is a pentagram. Why is there a presentation level <rank/> the last one in <ranknum/> is also marked with a digital rating? Maybe it's because there are other information in the database. In fact, we can find that the triangle, the pentagram are a picture, ok! What are their filenames? is the <ranknum/> text value plus the suffix. gif. Oh, for example, the picture of a triangle is user1.gif. In this way, when parsing with XSLT, it is possible to provide directly to the picture information, it is not to kill the birds. The content node is stored in CDATA without much to say.
Let's take a look at the implementation of the Release topic section:
Beginning:
<?xml version= "1.0" encoding= "UTF-8"?>
<xsl:stylesheet version= "1.0" xmlns:xsl= " Http://www.w3.org/1999/XSL/Transform "Xmlns:fo= "' target=_blank> Http://www.w3.org/1999/XSL/Format ">
I was using XMLSpy, which generated an. xsl file, so I added an fo namespace and we didn't care about it. Because we want to generate HTML files, (note: xml+xsl generated by default is UTF encoding), if you want to specify the encoding format, we should write the following sentence: <xsl:output method= "html" encoding= "GB2312" />. Because XML or HTML is generated by default when parsing, you can also specify the type of file that is generated.
We continue:
<xsl:template match= "Topic" >
<link rel= "stylesheet" href= "Topic.css" type= "Text/css"/>
<xsl:apply-templates select= "Issue"/>
<xsl:apply-templates select= "Replys"/>
<xsl:call-template name= "Setreplyframe"/>
</xsl:template>

This is our main function, and the entrance is right here. Why not Match= "/", because we are mainly topic this node, the rest do not need to take care of it. Match= "/" does not match topic, this should be noted!
In order to make our pages look better, of course, to use CSS style sheet (my aesthetic is very poor) hehe. The direct use of <link rel= "stylesheet" href= "Topic.css" type= "Text/css"/> In the parsing process will directly output the statement to the HTML page, which is why we can use HTML tags.
<xsl:apply-templates select= "Issue"/> This sentence indicates that the Issue can be matched to an available template. Some people here have questions, before you said apply-templates just matching child nodes? Why does this place become a issue again AH. Yes, if the Select attribute is not used in xsl:apply-templates, then all child nodes under the current node are matched, but if the select is used then the selected node is processed.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.