XML + XSL + CSS + ASP creates a message book

Source: Internet
Author: User
Tags xsl xsl file

Some time ago, I accidentally saw that a blog's RSS could be output in the XSL format and browsed in Firefox. I thought that an XML guestbook I wrote earlier was lost because it was incompatible with Firefox, now I am curious to see what he can browse in Firefox.CodeFinally, I found the cause and completed the message book. Because it is a simple XML message book, it is named sxgb (simple XML guestbook ).

Message book demo, the management password is test: http://home.goofar.com/hotheart/gbook/gbook.asp

First, define the format of the XML document in the message book. As a message book, there is no need for complicated content, so I divide the message content into three parts: the name of the message owner, the homepage of the message owner, and the message content. In addition, a message book also requires user information, including the user name and user homepage. In addition, paging information is required when many messages are sent. After the general structure is complete, you can start to write the XML document template.

The root element of the XML document is defined as gbook.
XML document template gbook. xml

Code:
XML version = "1.0" encoding = "UTF-8"?>
<! -- DTD file -->
<! Doctype gbook System "sxgb. DTD">
<! -- XSL file -->
<? XML-stylesheet type = "text/XSL" href = "gbook. XSL"?>
<Gbook>
<! -- Message book-related information -->
<Info>
<! -- Username -->
<User> hotheart </user>
<! -- User homepage -->
<Home> http://www.xujiwei.cn </Home>
<! -- Page information, which is the current page, total page number, previous page, and next page -->
<Pagenow> 1 </pagenow>
<Pagetotal> 1 </pagetotal>
<Pageprev> 1 </pageprev>
<Pagenext> 2 </pagenext>
<! -- Whether the log is logged in, used to check whether the logon box is displayed -->
<Logined> NO </logined>
</INFO>
<! -- Message list -->
<Messages>
<! -- A message -->
<Message>
<! -- Message id -->
<ID> 1 </ID>
<! -- Message holder name -->
<Username> admin </username>
<! -- Message time -->
<Time> </time>
<! -- Message holder homepage -->
<Homepage> http://www.xujiwei.cn/<! -- Message content -->
<Content> <! [CDATA [message content]> </content>
</Message>
</Messages>
</Gbook>

Note that the XSL cannot be referenced.
<? XML: stylesheet type = "text/XSL" href = "gbook. XSL"?>
XML and stylesheet should use a bar (-) instead of a colon (:), which is not supported in Firefox.

A good XML document must be structured and valid. Therefore, the document type definition (DTD) sxgb is defined at the beginning of the XML document. DTD. The document type is defined below. Because the structure of the XML document in the guestbook has been designed, It is very convenient to write the DTD.

Document Type Definition sxgb. DTD

code:


















<! In element messages (Message +)>, "+" indicates that at least one message exists in the message list. However, in actual application, there may be no message. To cope with this situation, ASP in the output XML documentProgramOutput A message generated by the system, prompting that no message is sent.

The next step is the compilation of XSL.

XSL document gbook. XSL
Click here to view the XSL document gbook. XSL

When defining an XSL namespace, use http://www.w3.org/5o/#/transform, that is

Code:
XSL: stylesheet xmlns: XSL = "http://www.w3.org/1999/XSL/Transform" version = "1.0">

However, you cannot use http://www.w3.org/tr/wd-example. If you use this namespace, firefoxwill display an error: An error occurred while analyzing the XSLT style form.

In XSL, a very important concept is the template. A template corresponds to the formatted output of a piece of content. When creating a template, you can create a global template first, and then process the contents of each segment, you can also build templates for subdivided content from small to large, and then combine them to form an overall template. In XSL, create a template and use the following tag:
[Code [<XSL: template match = "/gbook">
<! -- Template content -->
</XSL: Template> [/Code]
Match indicates the tag corresponding to the template.

There are two methods to use the template:

The first is direct application:

Code:
<XSL: Apply-templates select = "info"/>

This method selects the info tag under the current tag and uses the template with match as info for processing.

The second option is loop selection:

Code:
<XSL: For-each select = "message">
<! -- Template content -->
</XSL: For-eace>

In the loop selection, the template is directly written in for-each, but the current context has been transferred to message.

The expressions used in select are part of xpath. For details, refer to the W3C XPath document (XML Path Language). The tags used in gbook. XSL are:

<XSL: value-of select = "content"/>
This is one of the commonly used tags in XSL. The output result is to select the value of a node and use it to output the value of a node. In this message book, it is used to output user name, message holder name, and message content.

<XSL: attribute name = "href"> gbook. asp? Page = 1 </XSL: attribute>
Used to add an attribute to the current tag and add a URL to the paging navigation bar in the message book. "Name" indicates the name of the property to be added, and the content marked as the value of the property to be added.

<XSL: If test = "ID! = ''"> <! -- Process content --> </XSL: If>
Judge the expression result in test. If it is true, the contained content is processed. If it is false, the content is ignored. It should be noted that, if the value is less than, "<" cannot be used, but "<" should be used, because "<" is the start mark of the tag, if no conversion is performed, an error occurs in the browser.

Then there is the XML document required for ASP to output the message book. You only need to read the records from the database in the format defined above and output the records according to the template. However, you need to define the MIME type before the output:
Response. contenttype = "application/XML"
Indicates that this is an XML document. In addition, because the entire message book uses the UTF-8 encoding, You need to specify the encoding in ASP:
CodePage = "65001"
This statement should start with an ASP file.

Finally, there is CSS, which defines the Display Effect of the message book. For details, you can view the CSS content. In addition, to prevent users from submitting empty forms by mistake, I added a piece of JS Code on the client to check whether the message name and content are empty, if it is null, the system prompts the user and stops submitting the form. You can view the JS script file for details.

Complete Message book system sxgb package download

References:
1. xml and WAP development manual (edited by Li Jianguo)
2. XML, XSL, and CSS

Related Article

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.