Displaying Source Code(s)
|
|
XML Advertising
--------------------------------------------------------------------------------
Ok; firstly, we have To identify the information we want To
store. We need the actual advertising image, where the click Is
To go To (target), And To keep proper HTML an ALTernative tag.
Then we need To store the important information such As
impressions And click through. So, we End up With a relatively
simply XML structure.
<advertlist>
<advert clicks="0" impressions="0">
<id />
<url />
<image />
<alt />
<datecreated />
</advert>
</advertlist>
You'll also see that I've added a datecreated field, and a
rather important ID field. The datecreated field is simply for
information purposes. However, the ID field is used to uniquely
identify adverts. This is used for efficiency, and to uniquely
identify adverts quickly and easily!
A typically populated file may be:
<advertlist>
<advert clicks="0" impressions="0">
<id>1</id>
<url />
<image />
<alt />
<datecreated />
</advert>
</advertlist>
Now that we have defined the information we need, Next we need
To define a way To display it. By using XSL this Is made
relatively simple:
<render id="displayadvert">
<html>
<head>
<title>XML Advertising</title>
<meta name="Author" content="George Leithead" />
<meta name="AuthorContact" content="Code.Samples@InternetWideWorld.com"
/>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0"
summary="Advert and Home page">
<tr>
<td><A TARGET="_top"><xsl:attribute name="HREF">RecordMe.asp?id=<xsl:value-of
Select="id"/>&url=<xsl:value-of Select="url"/>&image=<xsl:value-of
Select="image"/></xsl:attribute><IMG BORDER="1" WIDTH="468"
height="60"><xsl:attribute name="SRC"><xsl:value-of
Select="image"/></xsl:attribute><xsl:attribute name="ALT"><xsl:value-of
Select="alt"/></xsl:attribute></IMG></A><img src="http://forums.internetwideworld.com/Images/Blank.gif"
width="1" height="1" alt="Weavel" /></td>
</tr>
</table>
</body>
</html>
</render>
Now we have our information, And a template To display it In, we
Now need To Select an advert And display it using the XSL. In
order To Do this, firstly we need Do implement a couple of
functions. The first Is To load the actual XML source And the
XSL style. Then we need a Function that transforms the XML And
XSL To produce our required HTML.
' Define where the XML and XSL files can be found.
' Usually, put these two lines in the GLOBAL.ASA file!
Application("AdPathXML") = Server.MapPath("Adverts.xml") ' Where
the adverts are!
Application("AdPathXSL") = Server.MapPath("Advertising.xsl") '
Where the adverts are!
Function LoadAdverts()
' Create the links to the XML and XSL files
Dim oSource
Dim oStyle
Set oSource = Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
oSource.async = False
oSource.load Application("AdPathXML") ' Load the XML file!!
Set oStyle = Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
oStyle.async = False
oStyle.load Application("AdPathXSL") ' Load the XSL file!!
Application.Lock
Set Application("Advertsource") = oSource
Set Application("Advertstyle") = oStyle
Application.UnLock
' Tidy Up!!!
Set oSource = Nothing
Set oStyle = Nothing
End Function
Function DisplayAdvert(iID, sRender)
' Display a selected advert in the selected render style
' Simply supply the Advert ID and the render
Dim oRequiredNode
Dim oSource
Dim oStyle
Dim oOutNode
Set oStyle = Application("Advertstyle")
Set oSource = Application("Advertsource")
Set oRequiredNode = oSource.selectSingleNode("advertlist/advert[id[.
= """ & iID & """]]")
If Not oRequiredNode Is Nothing Then
Set oOutNode = oStyle.selectSingleNode("xsl:stylesheet/render[@id="""
& sRender & """]")
If Not oOutNode Is Nothing Then
' Apply the style to the source!
DisplayAdvert = oRequiredNode.transformNode(oOutNode)
Else
DisplayAdvert = "ERROR: Either the XSL file is incorrect or the
render identifier is not found"
End If
Else
DisplayAdvert = "ERROR: Either the advert identifier supplied is
invalid of not found"
End If
' Tidy Up!!!
Set oOutNode = Nothing
Set oRequiredNode = Nothing
Set oStyle = Nothing
Set oSource = Nothing
End Function
You'll notice that the DisplayAdvert function accepts two
arguments, iID and sRender. The iID argument is the ID of the
advert we wish to display, and the sRender argument allows us to
specify the XSL style sheet we want to apply. That is, we can
display the advert in a number of formats.
So, we Now need To obtain the advert ID that we want To display.
I've done this by randomly selecting a number between one and
the maximum number of adverts that are available. Below you can
see the code to do this. You'll also notice that there is some
code in there to make sure that the selected advert ID actually
exists, and if not, we select another random advert id. The code
below is the actual advertising page.
<%@ Language=VBScript %><%
Option EXPLICIT%>
<!--#INCLUDE FILE="Display_Advertising.inc.asp"--><%
Dim oRoot ' The XML Root Object
Dim oNode ' An XML Node object
Dim iMaxID: iMaxID= 0 ' The Maximum ID of an Advert within the
Root!
Dim iAdvertNum: iAdvertNum= 1 ' The actual advert ID we are
going to show!
Dim bAdvertExists : bAdvertExists = False ' By default the
advert does not exist!
Dim iTimes: iTimes= 0 ' Record hw many times we've been round
the loop!
Dim iMaxTimes : iMaxTimes = 10' Maximum number of attempts
before giving up!
Dim sOutput: sOutput= ""' The output of the transformation
' Get the Advert source!
Set oRoot = Application("Advertsource")
' Get the ROOT Node!
Set oNode = oRoot.selectSingleNode("advertlist")
On Error Resume Next
' Get the MAX ID of the Nodes within the root!
iMaxID = CLng(oNode.lastChild.selectSingleNode("id").text) '
Make it a number!
If Err.number <> 0 Then
' Saftey check!
' The Node does NOT contain any records!!!
iMaxID = 0
Err.Clear()
End If
On Error goto 0
Set oNode = Nothing ' Tidy Up!!!
Set oRoot = Nothing ' Tidy Up!!!
' Only IF there is an advert, do we want to get it!
If iMaxID > 0 Then
While (Not bAdvertExists)
iTimes = iTimes + 1 ' Add 1 to the loop counter!
Randomize()
iAdvertNum = Int(iMaxID * Rnd + 1) ' Get an Advert ID Number
' Obviously, we need to ensure that the advert actually
EXISTS!!!
Set oNode = getAdvertFromID(iAdvertNum) ' Get the Node!
If oNode Is Nothing Then
' This Advert ID does NOT exist!!!
'Response.Write("The advert number " & CStr(iAdvertNum) & " does
not exist!")
Else
bAdvertExists = True ' The advert DOES exist!
Call RecordImpression(iAdvertNum) ' Need to record the view
impression!!!!
sOutput = DisplayAdvert(iAdvertNum, "displayadvert")
Response.Write(sOutput) ' Transform the XML output!
End If
Set oNode = Nothing ' Tidy Up!!!
If iTimes > iMaxTimes Then bAdvertExists = True ' Limit the
number of tries to 10...DON'T want to INFINATE loop!!!
Wend
If (Not bAdvertExists) Then
Response.Write("No Adverts defined!")
End If
End If%> |
|
|