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:



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:

 <%'response.expires=-1'processUtils.aspfile=request("file")class CVisitors   function updateVisitors(nodeList,pageName)      set nodeDoc=nodeList.item(0)      set node=nodeDoc.documentElement.selectSingleNode _      ("//counter[@name='"+pageName+"']")      if node is nothing then          set node=nodeDoc.documentElement.appendChild _          (nodeDoc.createElement("counter"))            node.setAttribute "name",pageName         node.setAttribute "visitors",0      end if      visitorCt=clng(node.getAttribute("visitors"))      node.setAttribute "visitors",cstr(visitorCt+1)       updateVisitors=node.getAttribute("visitors")   end functionend classif not isObject(utils) then   set utilsDoc=createObject("MSXML2.FreeThreadedDOMDocument")   set template=createObject("MSXML2.XSLTemplate")   utilsDoc.load server.mapPath("processUtils.xsl")   set template.stylesheet=utilsDoc   set utils=template.createProcessor   set session("utils")=utils   utils.output=response   set visitors=new CVisitors   utils.addObject Visitors,""else   set utils=session("utils")end ifset xmlDoc=createObject("MSXML2.FreeThreadedDOMDocument")xmlDoc.load server.mapPath(file)utils.input=xmlDocset counterDoc=createObject("MSXML2.FreeThreadedDOMDocument")counterDoc.load server.mapPath("counters.xml")utils.addParameter "counterSource",counterDocutils.addParameter "pageName",fileutils.transform server.mapPath("counters.xml")%>

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, 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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of