Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 26 - Case Study: Active Server Pages and XML Outline 26.1 Introduction 26.2 Setup and Message.

Similar presentations


Presentation on theme: " 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 26 - Case Study: Active Server Pages and XML Outline 26.1 Introduction 26.2 Setup and Message."— Presentation transcript:

1  2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 26 - Case Study: Active Server Pages and XML Outline 26.1 Introduction 26.2 Setup and Message Forum Documents 26.3 Forum Navigation 26.4 Adding Forums 26.5 Forum XML Documents 26.6 Posting Messages 26.7 Other Documents 26.8 Internet and World Wide Web Resources

2  2001 Prentice Hall, Inc. All rights reserved. 2 26.2 Setup and Message Forum Documents

3  2001 Prentice Hall, Inc. All rights reserved. 3 26.3 Forum Navigation Fig. 26.2Key interactions between message forum documents.

4  2001 Prentice Hall, Inc. All rights reserved. Outline 4 Forums.xml 1 2 3 4 5 6 7 8 ASP 9 10 Name of forum. XML document that contains the information for forum ASP.

5  2001 Prentice Hall, Inc. All rights reserved. Outline 5 Default.asp 1 2 3 <% ' Fig. 26.4 : default.asp 4 ' Forum home page 5 Option Explicit 6 %> 7 8 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 9 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 10 11 12 13 14 Deitel Message Forums 15 <link rel = "stylesheet" type = "text/css" 16 href = "style.css" /> 17 18 19 20 Deitel Message Forums 21 Available Forums 22 23 <% 24 Dim xmlFile, xmlNodes, xmlItem 25 Dim strPath, strTitle, strFileName 26 27 strPath = Server.MapPath( "forums.xml" ) 28 29 Set xmlFile = Server.CreateObject( "Microsoft.XMLDOM" ) 30 xmlFile.Async = False 31 32 If Not xmlFile.Load( strPath ) Then 33 Call Server.Transfer( "invalid.html" ) 34 End If 35 Link to CSS style sheet style.css. Instantiate an XML DOM object. Setting Async to False causes the object referenced by xmlFile to behave synchronously. Method Load parses the XML document forums.xml.If the parsing fails, the browser is redirected to invalid.html.

6  2001 Prentice Hall, Inc. All rights reserved. Outline 6 Default.asp 36 Set xmlNodes = xmlFile.DocumentElement.ChildNodes 37 38 For Each xmlItem In xmlNodes 39 strFileName = xmlItem.getAttribute( "filename" ) 40 strTitle = xmlItem.text 41 %> 42 43 "> 44 45 <% 46 Next 47 %> 48 49 50 Forum Management 51 52 53 Add a Forum 54 Delete a Forum 55 56 57 58 59 Property DocumentElement gets the root element’s child nodes. Property ChildNodes returns a collection of the element node’s child nodes. Method getAttribute gets a forum’s filename. The anchor element creates a link to the available forums. This anchor element links to the ASP document addForum.asp that allows the user to create a new forum.

7  2001 Prentice Hall, Inc. All rights reserved. Outline 7 Program Output

8  2001 Prentice Hall, Inc. All rights reserved. Outline 8 Template.xml 1 2 3 4 5 6 7 8 9 The stylesheet processing instruction references formatting.xsl.

9  2001 Prentice Hall, Inc. All rights reserved. Outline 9 AddForum.asp 1 2 3 4 5 6 <% 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 24 If xmlFile.Load( strPath ) Then 25 Call Server.Transfer( "invalid.html" ) 26 End If 27 28 ' set up the file 29 Call xmlFile.Load( Server.MapPath( "template.xml" ) ) 30 31 ' get the root element 32 Set xmlRoot = xmlFile.DocumentElement 33 Test to see if the form was submitted by testing the form’s Submit field for a value. Check to see if a value was enetered in each of the form fields. Method Load loads the template XML document.

10  2001 Prentice Hall, Inc. All rights reserved. Outline 10 AddForum.asp 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 xmlRoot.AppendChild( xmlNode ) 42 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 49 50 ' create user node 51 Set xmlNode = xmlFile.CreateElement( "user" ) 52 xmlNode.Text = Request( "user" ) 53 Call xmlRoot.AppendChild( xmlNode ) 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 ) 64 65 Call xmlFile.Save( strPath ) ' save the file 66 67 ' load XML file 68 strPath = Server.MapPath( "forums.xml" ) Method setAttribute creates an attribute node named filename that has the value contained in form field filename. Method AppendChild appends the newly created element name node to the root element. Calling method Save saves the XML document to disk.

11  2001 Prentice Hall, Inc. All rights reserved. Outline 11 AddForum.asp 69 70 Set xmlFile = Server.CreateObject( "Microsoft.XMLDOM" ) 71 xmlFile.Async = False 72 73 If Not xmlFile.Load( strPath ) Then 74 Call Server.Transfer( "invalid.html" ) 75 End If 76 77 ' get the root node 78 Set xmlRoot = xmlFile.DocumentElement 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 ) 86 87 Call xmlFile.Save( strPath ) ' save the file 88 89 ' finished processing 90 Call Server.Transfer( "default.asp" ) 91 Else 92 strError = "ERROR: Invalid input." 93 End If 94 95 End If 96 %> 97 98 <!DOCTYPE html 99 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 100 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 101 Open XML document forums.xml. Modify forums.xml. Save forums.xml with its new contents to disk.

12  2001 Prentice Hall, Inc. All rights reserved. Outline 12 AddForum.asp 102 103 104 Add a Forum 105 106 107 108 109 Create a Forum 110 111 112 113 114 115 Forum Name: 116 <input type = "text" size = "40" name = "name" 117 value = " " /> 118 119 120 121 Forum File Name: 122 <input type = "text" size = "40" name = "filename" 123 value = " " /> 124 125 126 127 User: 128 <input type = "text" size = "40" name = "user" 129 value = " " /> 130 131 132 133 Message Title: 134 <input type = "text" size = "40" name = "title" 135 value = " " /> 136 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.

13  2001 Prentice Hall, Inc. All rights reserved. Outline 13 AddForum.asp 137 138 139 Message Text: 140 <textarea name = "text" cols = "40" 141 rows = "4"> 142 143 144 145 146 147 148 149 150 151 152 Return to Main Page 153 154 155 156 157 The Submit button submits the form and the values input in the form fields. The Clear button will delete all values in the form fields.

14  2001 Prentice Hall, Inc. All rights reserved. Outline 14 Program Output

15  2001 Prentice Hall, Inc. All rights reserved. Outline 15 ForumASP.xml 1 2 3 4 5 6 7 8 9 10 ASP Forum 11 12 13 D. Bug 14 I Love ASP! 15 Everyone should use ASP. 16 17 18 19 Ms. Quito 20 ASP and XML 21 What a powerful combination. Try it! 22 23 24 25 Sarge Ant 26 ASP 27 This army ant uses ASP in boot camp. 28 29 30 Name of forum. Each message element and its children contain the information for a post.

16  2001 Prentice Hall, Inc. All rights reserved. Outline 16 Formatting.xsl 1 2 3 4 5 6 <xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 <xsl:output method = "html" omit-xml-declaration = "no" 10 doctype-system = 11 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" 12 doctype-public = "-//W3C//DTD XHTML 1.0 Strict//EN" /> 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <link rel = "stylesheet" type = "text/css" 27 href = "style.css" /> 28 29 Element xsl:stylesheet is the XSLT document’s root element Attribute version specifies the XSLT version to which this style sheet conforms. The attribute xmlns creates a namespace prefix xsl. The xsl:output element specifies how the result tree is output. Attribute method is assigned xml, which specifies that an XML document is being output. Attribute omit-xml-declaration is assigned no, which results in an XML declaration in the result tree. This template that match es the XML document root ( / ). The asterisk selects element nodes. Link to the CSS style sheet style.css.

17  2001 Prentice Hall, Inc. All rights reserved. Outline 17 Formatting.xsl 30 31 32 <table width = "100%" cellspacing = "0" 33 cellpadding = "2"> 34 35 36 37 38 39 40 41 <table width = "100%" cellspacing = "0" 42 cellpadding = "2"> 43 <xsl:apply-templates 44 select = "message" /> 45 46 47 48 49 <xsl:attribute 50 name = "href">addPost.asp?file=<xsl:value-of 51 select = "@filename" /> 52 53 Post a Message 54 Return to Main Page 55 56 57 58 59 60 61 62 Get value of name element. Apply message template. Get the value of attribute filename.Begin template message.

18  2001 Prentice Hall, Inc. All rights reserved. Outline 18 Formatting.xsl 63 64 65 66 67 68 69 70 71 by 72 73 at 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 Get value of user element. Get value of attribute timestamp. Get value of text element.

19  2001 Prentice Hall, Inc. All rights reserved. Outline 19 ForumASP_transfo rmed.html 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 3 4 5 6 7 8 9 10 ASP Forum 11 12 13 14 15 16 17 ASP Forum 18 19 20 21 22 23 I Love ASP! 24 25 26 27 by 28 D. Bug 29 at 30 4/28/2001 2:50:34 PM EST 31 32 Forum title.Message title.Message author.Time and date posted.

20  2001 Prentice Hall, Inc. All rights reserved. Outline 20 ForumASP_transfo rmed.html 33 Everyone should use ASP. 34 35 36 ASP and XML 37 38 39 40 by 41 Ms. Quito 42 at 43 5/8/2001 11:09:54 AM EST 44 45 46 47 What a powerful combination. Try it! 48 49 50 ASP 51 52 53 54 by 55 Sarge Ant 56 at 57 5/15/2001 4:39:50 PM EST 58 59 60 61 This army ant uses ASP in boot camp. 62 63 64 Message text. Message title. Message author.Time and date posted. Message text. Message title.Message author.Time and date posted.Message text.

21  2001 Prentice Hall, Inc. All rights reserved. Outline 21 ForumASP_transfo rmed.html Program Output 65 66 Post a Message 67 68 Return to Main Page 69 70 71 72 73 Link to add a new post.

22  2001 Prentice Hall, Inc. All rights reserved. Outline 22 AddPost.asp 1 2 3 <% ' Fig. 26.10 : addPost.asp 4 ' ASP document for posting a message 5 6 Option Explicit 7 8 Dim xmlFile, xmlRoot, xmlNode 9 Dim strTitle, strError, strPath 10 11 If Request( "submit" ) <> Empty Then 12 13 If Request( "file" ) <> Empty And _ 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 If Not xmlFile.Load( strPath ) Then 24 Call Server.Transfer( "invalid.html" ) 25 End If 26 27 ' get the root node 28 Set xmlRoot = xmlFile.DocumentElement 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 Test to see if the form was submitted by testing the form’s Submit field for a value. Check to see if a value was enetered in each of the form fields. Method Load loads the forum XML document.Create a message node.Create a timeStamp attribute for the message node.

23  2001 Prentice Hall, Inc. All rights reserved. Outline 23 AddPost.asp 36 37 ' create user node 38 Call CreateElementNode( "user", "userName", xmlNode ) 39 40 ' create title node 41 Call CreateElementNode( "title", "messageTitle", xmlNode ) 42 43 ' create text node 44 Call CreateElementNode( "text", "messageText", xmlNode ) 45 46 Call xmlFile.Save( strPath ) ' save the file 47 48 ' finished processing 49 Call Server.Transfer( Request( "file" ) ) 50 Else 51 strError = "ERROR: Invalid input." 52 End If 53 54 End If 55 56 ' procedure that creates an element node 57 Sub CreateElementNode( elementName, formElement, node ) 58 Set xmlNode = xmlFile.CreateElement( elementName ) 59 xmlNode.Text = Request( formElement ) 60 Call xmlRoot.AppendChild( xmlNode ) 61 End Sub 62 %> 63 64 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 65 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 66 Create the children of the message node: user, title and text.

24  2001 Prentice Hall, Inc. All rights reserved. Outline 24 AddPost.asp 67 68 69 Post a Message 70 <link rel = "stylesheet" type = "text/css" 71 href = "style.css" /> 72 73 74 75 76 77 78 79 User: 80 <input type = "text" size = "40" name = "userName" 81 value = " " /> 82 83 84 85 Message Title: 86 <input type = "text" size = "40" name = "messageTitle" 87 value = " " /> 88 89 90 91 Message Text: 92 <textarea name = "messageText" cols = "40" 93 rows = "4"> 94 95 96 97 98 <input type = "hidden" name = "file" 99 value = " "/> 100 This form allows the user to create a post by entering the values into the form fields.

25  2001 Prentice Hall, Inc. All rights reserved. Outline 25 AddPost.asp Program Output 101 102 103 104 105 106 ">Return to Forum 107 108 109 110

26  2001 Prentice Hall, Inc. All rights reserved. 26 26.6 Posting Messages Fig. 26.11New forum on the message board.

27  2001 Prentice Hall, Inc. All rights reserved. 27 26.6 Posting Messages Fig. 26.12Initial content of the newly added forum.

28  2001 Prentice Hall, Inc. All rights reserved. 28 26.7 Other Documents Fig. 26.13Contents of the Internet and World Wide Web: 2nd Edition.

29  2001 Prentice Hall, Inc. All rights reserved. Outline 29 Invalid.html 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 3 4 5 6 7 8 9 10 Deitel Book Organization 11 <link rel = "stylesheet" type = "text/css" 12 href = "site.css" /> 13 14 15 16 Invalid Request. 17 18 Return to Main Page 19 20 21 Link that references CSS style sheet site.css.

30  2001 Prentice Hall, Inc. All rights reserved. Outline 30 Site.css 1 /* Fig. 26.15 : site.css */ 2 /* Stylesheet for XHTML documents */ 3 4 body 5 { 6 background: white; 7 color: black; 8 font-family: Arial, sans-serif; 9 font-size: 10pt; 10 } 11 12 a 13 { 14 background: transparent; 15 color: blue; 16 text-decoration: none; 17 } 18 19 a:hover 20 { 21 text-decoration: underline; 22 } 23 24 table 25 { 26 border-width: 1px; 27 border-style: solid; 28 } 29 30.forumTitle 31 { 32 background: lime; 33 color: black; 34 font-size: 12pt; 35 font-weight: bold; Define styles for the body element.Define styles for the anchor element.Define styles for the table element.

31  2001 Prentice Hall, Inc. All rights reserved. Outline 31 Site.css 36 text-align: center; 37 } 38 39.msgTitle 40 { 41 background: silver; 42 color: black; 43 font-size: 10pt; 44 font-weight: bold; 45 } 46 47.msgInfo 48 { 49 background: silver; 50 color: black; 51 font-size: 10pt; 52 } 53 54.msgPost 55 { 56 background: silver; 57 color: black; 58 font-size: 8pt; 59 } 60 61.msgText 62 { 63 font-size: 10pt; 64 padding-left: 10px; 65 } 66 67.date 68 { 69 font-size: 8pt; 70 }

32  2001 Prentice Hall, Inc. All rights reserved. Outline 32 Style.css 1 /* Fig. 26.16 : style.css */ 2 /* Stylesheet for forums */ 3 4 h1 5 { 6 color: #330099; 7 letter-spacing: 2px; 8 font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; 9 background-color: transparent; 10 } 11 12 h2 13 { 14 color: #6633FF; 15 font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; 16 font-size: small; 17 background-color: transparent; 18 } 19 20 p 21 { 22 font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; 23 color: #336666; 24 letter-spacing: 1px; 25 font-size: larger; 26 font-weight: bold; 27 background-color: transparent; 28 } 29 30 body 31 { 32 background-image: url(bug2.gif); 33 background-repeat: no-repeat; 34 margin-top: 5%; 35 background-position: 25%; Define styles for the h1 element.Define styles for the h2 element.Define styles for the p element.Define styles for the body element.

33  2001 Prentice Hall, Inc. All rights reserved. Outline 33 Style.css 36 margin-left: 10%; 37 } 38 39 li 40 { 41 font-family: "Courier New", Courier, monospace; 42 font-weight: bolder; 43 list-style-type: circle; 44 color: #3333FF; 45 background-color: transparent; 46 } 47 48 input 49 { 50 background-color: transparent; 51 color: #336666; 52 font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; 53 } 54 55 textarea 56 { 57 background-color: transparent; 58 color: #336666; 59 font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; 60 } 61 62.forumTitle 63 { 64 color: #FFFFCC; 65 font-size: 14pt; 66 font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; 67 text-align: center; 68 background-color: #6666CC; 69 } 70 Define styles for the li element.Define styles for the input element.Define styles for the textarea element.

34  2001 Prentice Hall, Inc. All rights reserved. Outline 34 Style.css 71.msgTitle 72 { 73 background: #FFFFCC; 74 color: black; 75 font-size: 10pt; 76 font-weight: bold; 77 } 78 79.msgInfo 80 { 81 background: #FFFFCC; 82 color: black; 83 font-size: 10pt; 84 } 85 86.msgPost 87 { 88 background: silver; 89 color: black; 90 font-size: 8pt; 91 } 92 93.msgText 94 { 95 font-size: 10pt; 96 padding-left: 10px; 97 } 98 99.date 100 { 101 font-size: 8pt; 102 }


Download ppt " 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 26 - Case Study: Active Server Pages and XML Outline 26.1 Introduction 26.2 Setup and Message."

Similar presentations


Ads by Google