1 / 17

ASP XML Adding Content to an XML file over the Web

ASP XML Adding Content to an XML file over the Web. Displaying XML content using ASP/XSLT/CSS. What is ASP. Server Side Technology Uses scripting languages such as VbScript Very similar in syntax to client side VBScript we used to navigate the DOM

Télécharger la présentation

ASP XML Adding Content to an XML file over the Web

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP XMLAdding Content to an XML file over the Web Displaying XML content using ASP/XSLT/CSS

  2. What is ASP • Server Side Technology • Uses scripting languages such as VbScript • Very similar in syntax to client side VBScript we used to navigate the DOM • Code executes on server and returns pure HTML to the client • Interpreted, not compiled • Requires Internet Information Server, Microsoft’s popular Web server

  3. What is ASP Part Two • The Response object is used to write information to the browser. • You could take all of your client side document.write statements and replace them with response.write statements to tailor your VBScript for ASP. • The Request object is used to gather information from the browser to send to ASP.

  4. What is ASP Part Three • The request object is primarily used to grab information the user entered into a Web form so it can be processed • Dim booktitlebooktitle = Request.form(“booktitle”) • In the example above, booktitle is the name given to the form field that contains the book title. • <input type=text name=booktitle>

  5. XML and ASP • XML files can be opened on the server if they are stored on the server. • Sometimes you won’t know the actual drive name/path to your server side files (This could be the case if you use a host such as Brinkster, Interland, etc. to host your site) • You can use the server object’s MapPath method to dynamically add the physical location of your files. • Dim myBookset myBook = Server.CreateObject("Microsoft.XMLDOM")myBook.load(Server.MapPath(“books.xml"))

  6. Dissecting Books.asp • On Error Resume Next • Allows processing to continue even if an error occurs. Otherwise the page will crash. • If Len(Request("title"))>0 Then • This will evaluate to true if the form is being submitted. If not, then the page is being loaded for the first time. • set myBooks = Server.CreateObject("Microsoft.XMLDOM") • Create instance of the DOM on the server

  7. Dissecting books.asp Part 2 • myBooks.load(Server.MapPath("books.xml")) • Just like the client side code except that Server.MapPath is used to find the path to the books file on the server. In our case the path will likely be c:\inetpub\wwwroot • Set bookNode = myBooks.createElement("book") • The createElement method creates a new XML element and gives it the name in parentheses. We create the book element first because it is the parent for the title, price, author elements.

  8. Dissecting Books.asp Part 3 • Set objAttrib = myBooks.createAttribute("ISBN") • Creates an attribute node. The book element has three attributes, ISBN, type, pages • objAttrib.Text = Request("ISBN") • Sets the attribute’s text to whatever the user entered in the form field which has the name value of “ISBN” • bookNode.Attributes.setNamedItem objAttrib • Adds the attribute to bookNode. Booknode points to the book root element. As a result, the attribute is added to the book element.

  9. Dissecting Books.asp Part 4 • Set bookNodeChild = myBooks.createElement("title") • Since the createElement method is called on myBooks, the pointer to the book element, the new element is a child of book rather than a child of the root element. • A new element named title is created. • bookNodeChild.Text = Request("title") • Sets the text of the node to the value entered by the user in the form • bookNode.appendChild bookNodeChild • Adds the element as a child to book. The element is now included in the node tree. • Response.Write err.Description • Write out the description of any error that occurred as a result of adding the title element to the node tree.

  10. Dissecting Books.asp Part 5 • myBooks.save(Server.MapPath("books.xml")) • Saves the books.xml file to the exact same location. The new elements are included in the file. • Saving can fail if the folder does not allow for opening and saving of files. • Response.Write "<H2>You have successfully added the new book to the repository</H2>“Response.Write "<a href='viewbooks.asp'>View</a> all of the books in the repository." • Lets the user know that the book has been added and provides them with a link to visit a page that will display all of the books in the repository, including their newly added entry.

  11. Dissecting Books.asp Part 6 • Else'Page displayed for first time%> • ASP code must be included in <% and %> tags. Since we’re moving from ASP to HTML (ASP pages often contain a mixture of ASP and HTML code) we close the ASP tag. • Else is used to indicate what we should do if the form has not yet been submitted.

  12. Dissecting Books.asp Part 7 • <form method="POST" action="books.asp"> • The HTML form tag indicates that information is being submitted to the SAME page, books.asp. As a result, we need to check and see if the form should be displayed or processed. • Method=Post indicates that the form data should be submitted in the HTTP headers. If we chose method=get, the form’s name/value pairs would have been included in the page URL in the form of a querystring.

  13. Dissecting Books.asp Part 8 • <%End If%> • This is required at the end of the page because If/Then statements need to end with “End If” • The <% %> tags surround End If because it is part of the ASP script, not the HTML.

  14. Dissecting ViewBooks.asp Part 1 • Dim myBooks 'Create instance of DOM set myBooks = Server.CreateObject("Microsoft.XMLDOM") 'Force file to load all at once myBooks.async="false" 'Load books XML file myBooks.load(Server.MapPath("books.xml")) • 'Load books XSLT file Dim myStyle set myStyle = Server.CreateObject("Microsoft.XMLDOM") myStyle.async="false" myStyle.load(Server.MapPath("books.xslt")) • Both the XML file AND the xslt file are loaded into memory

  15. Dissecting ViewBooks.asp Part 2 • Dim ResultTreeResultTree = myBooks.transformNode(myStyle) • The transformNode method is called on the documentElement. The method takes one argument, the XSLT file that should be applied to the XML document. • The transformNode method returns a String of HTML. The string contains the result tree generated by processing the stylesheet.

  16. Dissecting ViewBooks.asp Part 3 • Response.Write ResultTree • Since ResultTree is a string of HTML, calling Response.Write will write out the result tree.

  17. Dissecting ViewBooks.asp Part 4 • We also applied a CSS stylesheet to Viewbooks.asp to format the table generated by the XSLT Transformation. • The code to add the stylesheet is added in the xslt file as part of the HTML output. • It appears in the <HEAD> of the output since that is where it would appear if you were writing straight HTML. • <link rel="stylesheet" type="text/css" href="books.css"/>

More Related