Browse DevX
Sign up for e-mail newsletters from DevX


Learn Better Ways to Manage Data in ASP  : Page 5

Take advantage of XML and XSLT to display, manage, and cache data.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Build the Administration User Interface
After a user elects to add or edit a link, the LinkEdit stylesheet creates the UI. The showEditForm() method retrieves the cached XSLTemplate object containing the stylesheet, creates a processor, and sets everything up for the transform. For existing link items, you pass the stylesheet the XML document and the id of the item the user wants to modify. As you can see, that's where the XSLProcessor object's addParameter() method comes in handyit's much simpler than retrieving an <xsl:param> node and setting its value using DOM methods.

In both cases, the methods set up the transform but defer running it until the response reaches the proper position in the HTML. The following HTML acts as a form container for the output of the various stylesheets:

<HTML> <HEAD> </HEAD> <BODY> <form name="frmEditMenu" method="post"> <% proc.transform if isEditMode then Response.Write "Edit the title and url and then click Submit.<br>" Response.Write "To delete a link, click Delete.<br>" end if Response.write proc.output %> </form> </BODY> </HTML>

After selecting a link, users can edit the title and the URL and submit the changes via the POST method by clicking the Submit button. When the request contains an update to an existing link item, the code first retrieves the item from the XML document and then assigns the updated title and URL strings to the title and URL attributes for that link element. Similarly, when a user deletes an item, the code retrieves the item and uses the DOM removeChild method to delete the item from the file. Those two operations are very straightforward.

The more interesting part happens when the user wants to add a new item. First, each tag contains a id attribute. The entire scheme rests on the ids being unique. One way to ensure unique values is to increment a number every time a user adds a new link element. The code accomplishes that by retrieving and incrementing the contents of the element at the top of the XML file every time a user clicks the Add button.

However, there's yet another problem. The XSLT stylesheet that displays the UI expects an id parameter, and selects that item from the XML filebut when users want to add a new item, the item doesn't exist yet. The simplest workaround is to create a new XML string containing a blank item and pass that to the stylesheet rather than the XML file containing the existing links. Here's the code:

Select Case Request.Form("btnAction") case "Delete" ' user clicked the Delete button ' code to delete an element here case "Add" ' user clicked the Add button ' get the next id value from the XML file set N = xml.selectSingleNode("menu/nextid") Response.Write N.text ' set the id to that value id = "link" & N.text ' update the nextid value in the XML file N.text = (cstr(cint(N.Text) + 1)) ' create a new link node in an empty menu file set blankXML = Server.CreateObject _ ("MSXML2.FreeThreadedDOMDocument.4.0") blankXML.loadXML "<?xml version=""1.0""?>" & _ "<menu><link id='" & id & "'" & " title='' " & _ "url=''/></menu>" ' show the blank edit form call showEditForm(id, blankXML) case "Submit" ' user clicked the Submit button ' code to update a link element's attributes here End Select

By using the blank XML document, if users click the Add button and then close the browser, the main XML document's <nextid> value updates, skipping a sequential value, but nothing else happens, which makes the technique relatively safe because no one cares about skipped id values.

Using these simple techniques, you can increase the speed of your Web site, let users safely modify dynamic data, eliminate syntax errors in the data file, and change the UI easily by simply updating the XSLT stylesheets that drive the transformations. After making a change to a stylesheet, an administrator can reload the page with the QueryString parameter "refresh=true" appended to the URLand all subsequent pages will reflect the changes.

A. Russell Jones , Ph.D., is the Executive Editor for DevX. Reach him by e-mail at rjones@devx.com.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date