This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
Overview
Download & View Case Study Asp And Xml as PDF for free.
Chapter 34 - Case Study: Active Server Pages and XML Outline 34.1 34.2 34.3 34.4 34.5 34.6 34.7 34.8
Introduction Setup and Message Forum Documents Forum Navigation Adding Forums Forum XML Documents Posting Messages Other Documents Web Resources
2004 Prentice Hall, Inc. All rights reserved.
1
2
Objectives • In this tutorial, you will learn: – To create a Web-based message forum using Active Server Pages. – To use XML with Active Server Pages. – To be able to add new forums. – To be able to post messages to the message forum. – To use Microsoft’s DOM to manipulate an XML document. – To use XSLT to transform XML documents.
2004 Prentice Hall, Inc. All rights reserved.
3
34.1 Introduction • Message forums – – – –
Popular internet feature Discuss topics Exchange information Several major sites provide this service • messages.yahoo.com/index.html • web.eesite.com/forums • groups.google.com
2004 Prentice Hall, Inc. All rights reserved.
4
34.2 Setup and Message Forum Documents • Prerequisites – – – –
Microsoft Internet Information Services 6 (IIS) Internet Explorer 6 (for XML and XSLT processing) MSXML 3.0 or higher Write permissions on forum folder
2004 Prentice Hall, Inc. All rights reserved.
5
34.2 Setup and Message Forum Documents File name forums.xml default.asp template.xml addForum.asp forumASP.xml formatting.xsl addPost.asp invalid.html site.css style.css
Fig. 34.1
Description XML document listing all available forums and their filenames. Main page, providing navigational links to the forums. Template for a message forum XML document. Adds a forum. Sample message forum. Document for transforming message forums into XHTML. Adds a message to a forum. Used to display an error message. Style sheet for formatting XHTML documents. Style sheet for formatting the message forum site.
Message forum documents.
2004 Prentice Hall, Inc. All rights reserved.
6
34.3 Forum Navigation Fig. 34.2
Key interactions between message forum documents.
default.asp
forums.xml addForum.asp
forumASP.xml formatting.xsl addPost.asp
2004 Prentice Hall, Inc. All rights reserved.
7
34.2 Setup and Message Forum Documents • Forum listing – XML – filename attribute
2004 Prentice Hall, Inc. All rights reserved.
1
Outline
2 3
4
forums.xml (1 of 1)
5 6
7 8
ASP
9 10
Name of forum.
XML document that contains the information for forum ASP.
2004 Prentice Hall, Inc. All rights reserved.
8
9
34.2 Setup and Message Forum Documents • Presenting forum listing – Convert to XHTML – CSS for formatting – ASP facilitates the conversion • DOMDocument
23 <% 24 Dim xmlFile, xmlNodes, xmlItem Instantiate an XML DOM object. 25 Dim strPath, strTitle, strFileName Setting Async to Load False causes the object referenced by xmlFile Method parses the XML document forums.xml. 26
Outline
27
strPath =
28
to behave synchronously. default.asp Server.MapPath( If the"forums.xml" parsing fails,) the browser is redirected to invalid.html . (2 of 3)
29
Set xmlFile = Server.CreateObject( "Microsoft.XMLDOM" )
30
xmlFile.Async = False
31 32 33 34
If Not xmlFile.Load( strPath ) Then Call Server.Transfer( "invalid.html" ) End If
35 36
Set xmlNodes = xmlFile.DocumentElement.ChildNodes
37 38 39 40 41 %> 42
For Each xmlItem In xmlNodes strFileName = xmlItem.getAttribute( "filename" ) strTitle Property= DocumentElement xmlItem.text
Method getAttribute gets a forum’s filename. The anchor element creates a link to the available forums. 2004 Prentice Hall, Inc. All rights reserved.
This anchor element links to the ASP document addForum.asp that allows the user to create a new forum.
2004 Prentice Hall, Inc. All rights reserved.
12
13
34.3 Forum Navigation Fig. 34.4
Message forums main page.
2004 Prentice Hall, Inc. All rights reserved.
14
34.3 Adding Forums • Template forum – Bare minimum elements of forum – stylesheet tag for formatting – forum tag
2004 Prentice Hall, Inc. All rights reserved.
1
Outline
2 3
4
template.xml
5 6
7 8
9
The stylesheet processing instruction references formatting.xsl.
2004 Prentice Hall, Inc. All rights reserved.
15
16
34.3 Adding Forums • Generating forum from template – Form to get information on new forum • Name and file name • User name • Message title and text
– Script to act on form input • Confirm valid input • SetAttribute – filename – timestamp • CreateElement • AppendChild • Save
2004 Prentice Hall, Inc. All rights reserved.
1
<% @LANGUAGE = "VBScript" %>
2
<% Option Explicit %>
3 4
Check to see if a value was entered in each of the form fields. addForum.asp Test to see if the form was submitted by testing the form’s (1 of 7) submit field for a value.
<% ' Fig. 34.6 : addForum.asp %>
5 6
Outline
<%
7
Dim xmlFile, xmlRoot, xmlNode
8
Dim strTitle, strError, strPath
9 10
If Request( "submit" ) <> Empty Then
11 12
If Request( "name" ) <> Empty And _
13
Request( "filename" ) <> Empty And _
14
Request( "user" ) <> Empty And _
15
Request( "title" ) <> Empty And _
16
Request( "text" ) <> Empty Then
17 18
' Create a new XML file
19
strPath = Server.MapPath( Request( "filename" ) )
20 21
Set xmlFile = Server.CreateObject( "Microsoft.XMLDOM" )
22
xmlFile.Async = False
23
2004 Prentice Hall, Inc. All rights reserved.
17
24 25 26
If xmlFile.Load( strPath ) Then Call Server.Transfer( "invalid.html" ) End If
Outline
27 28
' set up the file
29
Call xmlFile.Load(
addForum.asp Method Load loads the template XML document. (2 of 7) Server.MapPath( "template.xml" ) )
30 31
' get the root element
32
Set xmlRoot = xmlFile.DocumentElement
33 34
' set the filename
35
Call xmlRoot.SetAttribute( "filename", _
36
Request( "filename" ) )
37 38
' create Name node
39
Set xmlNode = xmlFile.CreateElement( "name" )
40
xmlNode.Text = Request( "name" )
41
Call
42
Method setAttribute creates an attribute node named filename that has the value contained in xmlRoot.AppendChild( xmlNode ) form field filename.
43
' create first message
44
Set xmlNode = xmlFile.CreateElement( "message" )
45
Call xmlNode.SetAttribute( "timestamp", Now & " EST" )
46
Call xmlRoot.AppendChild( xmlNode )
47 48
Set xmlRoot = xmlNode
Method AppendChild appends the newly created element name node to the root element. 2004 Prentice Hall, Inc. All rights reserved.
18
49 50
' create user node
51
Set xmlNode = xmlFile.CreateElement( "user" )
52
xmlNode.Text = Request( "user" )
53
Call xmlRoot.AppendChild( xmlNode )
Outline addForum.asp (3 of 7)
54 55
' create title node
56
Set xmlNode = xmlFile.CreateElement( "title" )
57
xmlNode.Text = Request( "title" )
58
Call xmlRoot.AppendChild( xmlNode )
59 60
' create text node
61
Set xmlNode = xmlFile.CreateElement( "text" )
62
xmlNode.Text = Request( "text" )
63
Call xmlRoot.AppendChild( xmlNode )
Calling method Save saves the XML document to disk.
64 65
Call xmlFile.Save( strPath ) ' save the file
66 67
' load XML file
68
strPath = Server.MapPath( "forums.xml" )
69 70
Set xmlFile = Server.CreateObject( "Microsoft.XMLDOM" )
71
xmlFile.Async = False
Open XML document forums.xml.
72
2004 Prentice Hall, Inc. All rights reserved.
19
73
If Not xmlFile.Load( strPath ) Then
74 75
Outline
Call Server.Transfer( "invalid.html" ) End If
76 77
' get the root node
78
Set xmlRoot = xmlFile.DocumentElement
addForum.asp (4 of 7)
79 80
' create nodes
81
Set xmlNode = xmlFile.CreateElement( "forum" )
82
Call xmlNode.SetAttribute( "filename", _
83
Request( "filename" ) )
84
xmlNode.Text = Request( "name" )
85
Call xmlRoot.AppendChild( xmlNode )
Modify forums.xml.
86 87
Call xmlFile.Save( strPath ) ' save the file
88 89
' finished processing
90
Call Server.Transfer( "default.asp" )
91
Else
92 93
strError = "ERROR: Invalid input." End If
94 95
Save forums.xml with its new contents to disk.
End If
96 %> 97
2004 Prentice Hall, Inc. All rights reserved.
20
98
Outline
101 102 103 104
Add a Forum
105
addForum.asp (5 of 7)
This XHTML form allows a user to input the name, filename, a user name, a message title and the message text to create a new forum.
106 107
108 109
Create a Forum
110
<% =strError %>
111 112
150 151 152 153
addForum.asp The Submit button submits the form and the (7 of 7) values input in the form fields.
34.5 Forum XML Documents • Sample forum – XML modified with several forums added – XSLT used to transform XML – Final XHTML page served to Internet Explorer
2004 Prentice Hall, Inc. All rights reserved.
1
2 3
4
name of the forum.
Outline forumASP.xml (1 of 2)
7 8 9 10
Each message
element and its children contain the information for a post.
Select the template that matches the XML document root (/). "-//W3C//DTD XHTML 1.0 Strict//EN" /> The asterisk selects element nodes. The attribute xmlns creates a namespace
Attribute version specifies the XSLT version to XSLT which thisXML style sheet conforms. root element. Attribute omit-xmlformatting.xsl The xsl:output element specifies how the result declaration is assigned no, (1 of 4) <xsl:stylesheet version = "1.0" tree is output. which results in an XML Attribute method is assigned html, which specifies that xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> declaration in the an result tree. document is being output. XHTML
prefix xsl.
<xsl:apply-templates select = "*" />
19 20
21 22
<xsl:template match = "forum">
23
2004 Prentice Hall, Inc. All rights reserved.
28
24
25
<xsl:value-of select = "name"/>
26
27 28
href = "style.css" />
Link to the CSS style sheet style.css. formatting.xsl (2 of 4)
Output of the transformation of the forum XML document.
2004 Prentice Hall, Inc. All rights reserved.
36
34.6 Posting Messages • Posting a message – Procedure similar to creating new forum
2004 Prentice Hall, Inc. All rights reserved.
1
<% @LANGUAGE = "VBScript" %>
Outline
2 3 4
<% ' Fig. 34.10 : addPost.asp ' ASP document for posting a message
5 6 7
Option
Check to see if a value was entered in each of the form fields. ExplicitTest to see if the form was submitted by testing the form’s submit field for a value.
8
Dim xmlFile, xmlRoot, xmlNode
9
Dim strTitle, strError, strPath
addPost.asp (1 of 5)
10 11
If Request( "submit" ) <> Empty Then
12 13
If Request( "file" ) <> Empty And _
Method Load loads the forum XML document.
14
Request( "userName" ) <> Empty And _
15
Request( "messageTitle" ) <> Empty And _
16
Request( "messageText" ) <> Empty Then
17 18
strPath = Server.MapPath( Request( "file" ) )
19 20
Set xmlFile = Server.CreateObject( "Microsoft.XMLDOM" )
21
xmlFile.Async = False
22 23 24 25
If Not xmlFile.Load( strPath ) Then Call Server.Transfer( "invalid.html" ) End If
2004 Prentice Hall, Inc. All rights reserved.
37
26 27
' get the root node
28
Set xmlRoot = xmlFile.DocumentElement
Outline
29 30
' create first message
31
Set xmlNode = xmlFile.CreateElement( "message" )
32
Call xmlNode.SetAttribute( "timestamp", Now & " EST" )
33
Call xmlRoot.AppendChild( xmlNode )
34 35
Set xmlRoot = xmlNode
addPost.asp Create a message (2 ofnode. 5)
Create a timeStamp attribute for the message node.