Code Samples -- VBScript

Example #8 - XML Processing
<%@ Language=VBScript %>
<%
Set oConn = Server.CreateObject("OpenX2.Connection")
Set oCommand = Server.CreateObject("OpenX2.Command")
Dim sResult, sSQL, sResultStr, sResultDoc
Dim bError
Dim i
sSQL = "SELECT * FROM titles"

On Error Goto 0

Sub ProcessErr
  If Err.number <> 0 Then
    bError = true
    sResult = ""
    If oConn.ErrorCode <> 0 Then
      sResult = "OpenX2 Connection Error: " & oConn.ErrorInfo & ". Error #" & oConn.ErrorCode & " (" & oConn.ErrorCodeEx & ")<br />"
    Else
      If oCommand.ErrorCode <> 0 Then
        sResult = "OpenX2 Command Error: " & oCommand.ErrorInfo & ". Error #" & oCommand.ErrorCode & " (" & oCommand.ErrorCodeEx & ")<br />"
      End If
    End If
    If sResult = "" Then
      Rem you may reRaise the Error here if you want to allow IIS process rest of errors
      sResult = "ASP Error: #" & CStr(Err.Number) & ". " & Err.description & "<br />"
    End If
  End If
End Sub

Sub ProcessQuery
  bError = false
  oConn.Connect("ms_ox1")
  oCommand.Connection = oConn
  oCommand.CommandText = sSQL
  sResultStr = oCommand.XMLString
  oCommand.Close()
  
  oCommand.CommandText = sSQL
  REM // Set stylesheet locations
  Dim styleFile
  Set xmlTmpl = Server.CreateObject("Msxml2.XSLTemplate")
  Set xslStyle = Server.CreateObject("Msxml2.FreeThreadedDOMDocument")
  styleFile = Server.MapPath("OpenX2.xsl")
  xslStyle.async = false
  xslStyle.load(styleFile)
  Set xmlTmpl.stylesheet = xslStyle
  Set xmlProc = xmlTmpl.createProcessor()
  REM // Get the XMLDocument from OpenX2
  xmlProc.input = oCommand.XMLDocument
  If xmlProc.transform() Then
  	sResultDoc = xmlProc.output
  End If
End Sub

REM // Main Processing
On Error Resume Next
ProcessQuery
ProcessErr

%>

<html>
  <head><title>OpenX2 Test #9 - XML Output</title></head>
<%
If Not bError Then
%>
  <XML id="source"><%=sResultStr%></XML>
  <XML id="style" src="openx2.xsl"></xml>
  <script type="text/javascript" event="onload" for="window">
    showResult.innerHTML = source.transformNode(style.XMLDocument);
  </script>
<%
End If
%>
  <body>
<%
If Not bError Then
%>
    <h1>Server-Side XMLDocument Processing</h1>
    <div><%=sResultDoc%></div>
    <h1>Client-Side XML-Island (String) Processing</h1>
    <div id="showResult"></div>
<%
Else
%>
  <div><%=sResultDoc%></div>
<%
End If
%>
<%= sResult %>
  </body>
</html>