Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Learn Better Ways to Manage Data in ASP  : Page 4

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

Add XML/XSLT-Based Administration
Creating the links table using cached data is measurably faster, but it has a problem. By loading the XML document from memory each time, you could change the link file and propagate the change to clients immediately. But by pulling the datafrom cache, changes you make to the file will not appear until you shut the application down and restart it. In other words, you need a way to know when to refresh the cached data. You can do that in several ways:

  • by checking the file date/time against a stored file date/time every time a page requests the file
  • by creating a method that checks a Request parameter, for example "RefreshMenu=true"
  • by creating an ASP page that lets the appropriate people update the fileand then automatically refreshes the data.
The last option is the best for non-administrative users, because using the administration page minimizes the possibility of a manually entered syntax error causing problems and eliminates the possibility of someone forgetting to update the cached data (via a Request parameter) after modifying the file. In addition, people who need to modify the data can update it from any browser that can reach the administration page. However, for administrators, it's generally a good idea to create a Request parameter that automatically refreshes the XML and XSLT files, so that they can make changes to the XSLT templates without having to shut down the site to force the changes to appear.

The people who maintain the list of links need to be able to add, modify, and delete tags within the XML file. XSLT isn't strictly required to build and display the user interface for list administration, but it simplifies and optimizes the code considerably, offloading otherwise messy and slow string operations in your VBScript code to the XSLT stylesheet instead.

Because the fields to add a new link are identical to the fields to modify an existing link you can use the same user interface for both operations. Therefore you only need two transformsone to display the list and one to display the data for editing, along with the appropriate controls. The following stylesheet displays the link list in alphabetical order by title. Note that you might extend this stylesheet to let users sort the links in other ways as well, for example, to display the links in the order they appear on the site (the code is in the file menuEditSelect.xsl).

<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html><head></head><body> <xsl:apply-templates select="menu"/> </body></html> </xsl:template> <xsl:template match="menu"> <table align="left" width="150"> <xsl:apply-templates select="link"> <xsl:sort select="@title"/> </xsl:apply-templates> <tr><td> <input type="submit" id="btnAdd" name="btnAction" style="width: 100;" value="Add"></input> </td></tr> </table> </xsl:template> <xsl:template match="link"> <tr><td> <a href="testMenu3.asp?id={@id}"> <xsl:value-of select="@title"/></a> </td></tr> </xsl:template> </xsl:stylesheet>

The first template in the stylesheet matches the root element. The second matches the <menu> element, selects its child <link> elements, and sorts them by the title attribute value. The last template displays the links as anchor tags. Finally, it displays an submit button with the caption "Add" at the end of the list. For longer lists, you'd want to place the button elsewhere, so it doesn't scroll off the screen.

When the user selects a tag, the anchor returns the id of the selected link as a QueryString parameter. The anchor tags perform "GET" requests, while a form with the method="post" attribute performs a "POST" request. You can use the request type to determine whether the user has posted new or altered data or wants to edit or insert a link.

select case Request.ServerVariables("REQUEST_METHOD") case "POST" ' perform update, add, and delete operations here case "GET" ' display the details for a link item end select

After determining the request type, you can find out whether the user has selected a link or is requesting the initial list display by checking for the existence of the id QueryString parameter. When the parameter is missing, the user has requested the list, and the code calls the showList() method; otherwise, the user has selected a link and wants to edit that item, so the code calls the showEditForm() method, passing the string value of the id parameter.

case "GET" ' display the details for a link item if Request.QueryString("id") = "" then call showList() else call showEditForm _ (Cstr(Request.QueryString("id")), xml) end if end select

The showList() method loads the menuEditSelect.xsl stylesheet.

sub showList() set template = Application("LinkSelect") set proc = template.createProcessor proc.input=xml end sub

The showEditForm() method also loads an XSLT template but needs to be able to select a single item (identified by id) in editable form; therefore it takes the id as the first parameter. The method also accepts an XML DOMDocument object (xmldoc) containing the source data.

sub showEditForm(id, xmldoc) set template = Application("LinkEdit") set proc = template.createProcessor proc.addParameter "id", id proc.input=xmldoc isEditMode=true end sub

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