Xml+xsl+css+asp to create a guest book _ Application Skills

Source: Internet
Author: User
Tags xpath xsl xsl file

Some time ago inadvertently see a blog RSS can be used in the XSL format output and can be browsed in Firefox, think of their previous written an XML guest book because it is not compatible with Firefox, and now see his ability to browse in Firefox feel very curious, looked at the code, A sentence of the comparison, and finally found the reason, but also to the completion of the guest book. Because it is a simple XML guest book, it is named SXGB (simply XML guestbook).

Message this demo, admin password for test:http://home.goofar.com/hotheart/gbook/gbook.asp

First, define the format of the XML document for the guest book. As a guest book, do not need too complex content, so I give the message content is divided into 3 parts: the name of the message, the home page and message content. In addition, a guest book also requires some information about the user, including the username and user homepage. Again, in the message more than a few pages of information required. After the approximate structure is complete, you can begin to write the XML document template.

The XML document root element 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>
<!--guest Book related information-->
<info>
<!--user name-->
<user>HotHeart</user>
<!--user Homepage-->
<!--pagination information, which is the current page, total number of pages, previous page, next page-->
<pagenow>1</pagenow>
<pagetotal>1</pagetotal>
<pageprev>1</pageprev>
<pagenext>2</pagenext>
<!--has been logged in to handle whether to display the landing box-->
<logined>NO</logined>
</info>
<!--message list-->
<messages>
<!--a message-->
<message>
<!--message ID-->
<id>1</id>
<!--message name-->
<username>Admin</username>
<!--message Time-->
<time>2005-08-09 12:00</time>
<!--Guestbook Home-->
<!--message content-->
<content><! [cdata[Message Content]]></content>
</message>
</messages>
</gbook>

Note that you cannot use a reference to the XSL
<?xml:stylesheet type= "text/xsl" href= "gbook.xsl"?>
XML and stylesheet should be separated by a single bar (-) instead of a colon (:), which is not supported by colons in Firefox.

A good XML document, in addition to the structure, should also be effective, so at the beginning of the XML document defines the document type definition (DTD) SXGB.DTD, the following is to define the document type to complete. Because the structure of the guest book XML document has been designed, it is convenient to write a DTD.

Document type definition Sxgb.dtd

code:
<?xml version= "1.0" encoding= "Utf-8"
<! ELEMENT Gbook (info,messages)
<! ELEMENT info (user,home,msgtotal,pagenow,pagetotal,pageprev,pagenext,logined)
<! ELEMENT messages (message+)
<! ELEMENT message (id,username,time,homepage,content)
<! ELEMENT User (#PCDATA)
<! ELEMENT Home (#PCDATA)
<! ELEMENT msgtotal (#PCDATA)
<! ELEMENT Pagenow (#PCDATA)
<! ELEMENT pagetotal (#PCDATA)
<! ELEMENT Pageprev (#PCDATA)
<! ELEMENT Pagenext (#PCDATA)
<! ELEMENT logined (#PCDATA)
<! ELEMENT ID (#PCDATA)
<! ELEMENT title (#PCDATA)
<! ELEMENT username (#PCDATA)
<! ELEMENT time (#PCDATA)
<! ELEMENT homepage (#PCDATA)
<! ELEMENT content (#PCDATA)

<! ELEMENT messages (message+) > + number indicates that there is at least one message in the message list, but the actual application may appear without a message, in order to deal with this situation, I output an ASP program of XML document output a system generated messages, prompted that there is no message.

Next is a very important part, the XSL is written.

XSL document Gbook.xsl
Click here to view the XSL document Gbook.xsl

When defining an XSL namespace, you should use Http://www.w3.org/1999/XSL/Transform, which is

CODE:
Xsl:stylesheet xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" version= "1.0" >

Instead of using http://www.w3.org/TR/WD-xsl, I don't know why if you use this namespace, you will see an error in Firefox: Parsing the XSLT style form failed.

In XSL, a very important concept is the template. A template corresponds to the formatted output of a piece of content, when you build a template, you can start from big to small, that is, build the global template first, and then process each piece of subdivided content, can also be small to large, first build a good subdivision of the template, and then combine them to form the overall template. In the XSL, create a template using the following tags:
[Code[<xsl:template match= "/gbook" >
<!--template content-->
</xsl:template>[/code]
Match shows that the template corresponds to that tag.

There are two ways to use templates:

The first is direct application:

CODE:
<xsl:apply-templates select= "Info"/>

This method is to select the info tag under the current tag and to use the template for info with match

The second is a circular selection:

CODE:
<xsl:for-each select= "Message" >
<!--template content-->
</xsl:for-eace>

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

For the expressions used in select, which belong to the XPath section, you can refer to the XPath document (XML Path Language) of the consortium . The tags that are also used in gbook.xsl are:

<xsl:value-of select= "Content"/>
This is one of the commonly used tags in the XSL, the output is to select the value of the node, by using it to output the value of the node, in this message, it is used to output the user name, message name, message content.

<xsl:attribute name= "href" >gbook.asp?page=1</xsl:attribute>
Used to add a property to the current tab, use it in the guest book to add a URL to the pager. Where the Name property represents the name of the property to add, and the tag contains the value of the property to be added.

<xsl:if test= "id!=" ><!--processing content--></xsl:if>
The expression in test is judged to be the result, and if it is true, the content is handled and the false is ignored. Note that if you are less than a judge, you cannot use "<" instead of "<" because "<" is the label start flag, and if you do not make a conversion, an error will occur in the browser.

Then is the ASP output guest book need XML document, this as long as the format defined previously, read records from the database according to the template output, but 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 guest book uses the UTF-8 encoding, you need to specify the encoding in the ASP:
Codepage= "65001"
This sentence should be placed at the beginning of the ASP file.

Finally there is a CSS, which belongs to the definition of the display of the guest book, you can view the content of the CSS. In addition, in order to prevent user error to submit an empty form, I added a paragraph of JS code on the client to check the message name and message content is empty, if it is empty to prompt the user and stop submitting the form, the specific content can view JS script files.

Complete Guest Book system SXGB package download

Resources:
1. XML and WAP Development manual (Jianguo, etc.)
2. XML, XSL,CSS Mess

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.