devxlogo

Add a Counter to Your Web Page With XHTML

Add a Counter to Your Web Page With XHTML

Browsers Targeted: Internet Explorer 3+, Netscape Navigator 3+

You can take advantage of the new XHTML standard in your own code, regardless of your browser, to enhance the capabilities of your Web pages. For example, page counters are common Web elements?so common that it would seem natural to express them as a tag. Because this information is server side, that tag expression would need to be interpreted on the server and replaced with the appropriate data. For example, suppose that you had an XHTML document such as visitors.htm:

 

Visitors

There have been visitors to this site.

You could then use an XSL filter (processUtils.xsl) to automatically handle any utils: tag within the source document. This uses the newer MSXML Preview parser to parse the XSL:

 

Additionally, you need two other files, an XML document to contain the counter (counter.xml):

 

and an ASP file to retrieve the file to be processed and produce the appropriate output, using an XSL processor:

 

Note in this particular case that the utils object has two parameters?the counterSource (which indicates the location of the counter.xml file) and the pageName (which uses the name of the file to act as a key to identify the proper element). It also incorporates an object from the CVisitor class, which updates the appropriate counter within the XSL script. Note that the object only needs to be associated with the XSL processor once.

Finally, this would be called from a command line by passing the filename of the XHTML file you want to transform as a query string argument to the processUtils.asp file?for example, http://www.vbxml.com/processUtils.asp?file=default.htm. Through this mechanism, you’ve created an alternative processing technique for files that uses ASP only to do the initialization and uses XML and XSL for everything else (the processUtils.xsl file takes on the role of ASP in its normal guise of creating fine-grained output).

This may seem like a lot of work for transforming one element (you could do this just as readily with a straight script) but if you had a number of utils: elements where each had its own associated mapping within the processUtils.xsl file, then the advantage rapidly shifts to the XML architecture.

devx-admin

Share the Post: