To read the article online, visit http://www.4GuysFromRolla.com/webtech/081300-1.2.shtml

Online Content Creation using ASP, XML and XSL, Part 2

By Arno Jansen


  • Read Part 1

  • In Part 1 we discussed the intent of this project. We also looked at the structure of the XML that contains our information. In this part we'll look at how to both view and edit this XML data.

    The view mode is not really interesting from the developers point of view, because it just parses the stylesheet and the XML file, creates HTML and that's it. The interesting part is when we are in edit mode. The edit.xsl stylesheet created a form that, after pressing the submit button, posts to savememo.asp. This savememo.asp file reads the values of the form and stores them in the XML file. Retrieving the values from the form is peanuts, we just use the Request.Form method, but once we have the values, we want to store them in the XML file, and that's where the magic comes in, so let's have a look at the savememo.asp file.

    We start again with creating an instance of the parser object and tell it what file to parse into an internal tree structure:

       Set objXML = Server.CreateObject("Microsoft.XMLDOM")
       objXML.Load(server.mappath("memo.xml"))
    

    Now, it would be great if we had a point in the file that we know, from there the structure of our memo is always the same. For this article, the memo is a structure of itself, but in more complex situations, you might find this in a more complex structure. So, instead of writing a recursive function that gives us the node in the tree where the memo starts, we use a method that was written for this purpose: getElementsByTagName. It expects a string of the tag you are looking for. If there are more tags found (a list of memo's), it returns a nodelist object of nodes where that tag was found.

       'Find the memo tags in the template
       Set memoLst = objXML.getElementsByTagName("memo")
    

    In this example we have only one memo, but as said, if there are more memo's in a file, a list of positions is returned. We can get the number of appearances of the specified tag:

      'Set the number of objects found
      intNoOfMemos = objLst.length
    

    You could loop through all of these memo's to do something with them, I will write another article on that later. For now, let's focus on one memo. Because we know there is only one memo in our file, we can harcode a pointer to that node in the tree:

      Set memoLocation = memoLst.item(0)

    After this the memoLocation variable will point to the part of the tree where the <MEMO> tag was found and all it's children (<FROM>, <TO>, etc). Okay, we have parsed the memo.xml file, so it's in internal memory in a tree structure, we know where in the tree the memo is and also we know how to get the values entered by the surfer:

      FromValue = request.form("from")
      ToValue = request.form("to")
      SubjectValue = request.form("subject")
      MessageValue = request.form("message")
    

    So the final step is to replace the original values from the file with the values the user entered. Let's see how we can accomplish that with the FROM tag:

      'Set a pointer to the current/old value in the document tree
      set OldFromNode = memoLocation.childNodes(0)
    

    The FROM tag is the first child-tag of the memo-tag in our memo.xml file and thus it is also the first childnode of the memo node in the tree. So we set the pointer to the from node. As you can see in the whole file, we do this same procedure for all tags and since we know their order of appearance (FROM, TO, SUBJECT, MESSAGE), we can set the pointers to the old values accordingly. The pointer to the old TO node will be: memoLocation.childNodes(1), the subject will be memoLocation.childNodes(2) and the message will be memoLocation.childNodes(3).

    We can just set the value of the tags, but to enhance the educational value of this example, we will do it differently. Separate from the tree structure, we will create our own little tree structure for each tag. So, we make an elementnode first:

      'Create a new  tag 
      '  (it is created but NOT yet added to the tree!!!)
      Set NewFromElement = objXML.createElement("from")
    

    And we create a separate textNode that contains the value the user entered:

      Set NewFromText = objXML.createTextNode(FromValue)

    Now we combine the elementNode we just made with the textnode we just made by appending the textnode to the element node:

      NewFromElement.appendChild(NewFromText)

    So now we have a separate little mini-tree structure containing a from tag and the new value, so the last step is to replace the part of the from tag in the old tree with the new one:

      memoLocation.replaceChild NewFromElement, OldFromNode

    And we replaced the old from value with the new one. The procedure for the other tags (TO, SUBJECT and MESSAGE) is exactly the same, except that we apply it to different childnodes, otherwise we would be replacing the from tag over and over again. So, as said, for the TO tag we point to memoLocation.childnodes(1), etc. After we have done this for all tags, we have a tree structure in the server's memory and the last step is to write that tree out to the memo.xml file on disk. And that is simply done by this line:

      objXML.Save(Server.mappath("memo.xml")

    And Voilá, you're done. Now, maybe you do not want to overwrite the old memofile but give surfers the opportunity to enter a filename and store it using the name they entered. That would require an extra formfield on the edit page with an unique name, which we can add to the edit.xsl stylesheet. On the save page (savememo.asp), we request the name the user entered and save it using that filename. That's pretty easy to do:

      filenamesave = Request.Form("saveas")
      objXML.Save(Server.mappath("/") & "\" & filenamesave)
    

    And you're ready to roll! You can download the complete sourcecode of this example and if you want; for more detailed information on the used stylesheets read the article: Translating XML into HTML with XSL.

    Future plans:
    Of course the memo is just an example for this article. In real life I created a complete online content creation tool for the company I work for. It allows users to write their own XML (or HTML) files based on templates I wrote. Those templates also contain ordered and unordered lists and I got some feedback from users that they want to be able to use the template as it is, but be able to add and delete bulletpoints. Also they would like to add and delete paragraphs of text. I am still working on that challenge, but the end is near. And I will write another article on how to do that!

    Happy Programming!

  • Arno Jansen


    Attachments:

  • Download the stylesheets, XML files, and ASP pages discussed in this article (in ZIP format)
  • Using XSL Stylesheets to Translate XML into HTML


  • Article Information
    Article Title: Online Content Creation using ASP, XML and XSL, Part 2
    Article Author: Arno Jansen
    Published Date: Sunday, August 13, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/081300-1.2.shtml


    Copyright 2014 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers