Link to home
Create AccountLog in
Avatar of Pete_Zed
Pete_ZedFlag for New Zealand

asked on

How do I convert XML into HTML via classic ASP?

I have an XML feed:

http://www.fishpond.co.nz/Movies/Blu_ray/Animated/xml.php?ref=1006

Which I wish to use in a classic ASP page. How do I convert the XML output into an HTML or classic ASP page? Step by step instructions would be gratefully accepted.
Avatar of zc2
zc2
Flag of United States of America image

You need create an XSLT transformation, and use the following ASP code to do the transform:
<%
option explicit
 
dim objXSL 
dim objXML
Dim xslt
Dim xslProc
 
set objXML = Server.CreateObject("MSXML2.DOMDocument")
if not objXML.load("http://www.fishpond.co.nz/Movies/Blu_ray/Animated/xml.php?ref=1006") then
        Response.Write "Error loading data"
        Response.Write "<br>Error number: " & err.number 
        Response.Write "<br>Description: " & err.Description
        Response.end
end if
 
'create XSL template
set xslt = server.CreateObject("Msxml2.XSLTemplate")
 
set objXSL = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
if objXSL.load(Server.MapPath("Feed2XHTML.xsl")) then
        xslt.stylesheet = objXSL
else
        Response.Write "Error loading template"
        Response.Write "<br>Error number: " & err.number 
        Response.Write "<br>Description: " & err.Description
        Response.end
end if
 
'Transform to XHTML
Set xslProc = xslt.createProcessor()
xslProc.input = objXML
xslProc.Transform
Response.ContentType = "text/xhtml"
Response.Write xslproc.output
 
set objxml = nothing
set objxsl = nothing
%>

Open in new window

The XSLT transformation I mentioned above and referred in the ASP code as the file named "Feed2XHTML.xsl" could be like follows (it's very simple, but you gat the idea):
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html" encoding="iso-8859-1"/>
	<xsl:template match="/">
      <HTML><BODY>
		<xsl:apply-templates select="STORE/PRODUCTS/PRODUCT"/>
	  </BODY></HTML>
	</xsl:template>
		
	<xsl:template match="PRODUCT">
	   <DIV style="clear: both; margin-top:2em; border-top: solid 1px red;">
		<IMG src="{IMAGE}" align="left"/>
		<A href="{PRODUCT_URL}"><xsl:value-of select="ATTRIBUTE1"/></A>
		<xsl:if test="string-length(author)">
			by <xsl:value-of select="author"/>
  		</xsl:if>
		<BR/>ISBN: <xsl:value-of select="isbn"/>
		<BR/>Price: <xsl:value-of select="PRICE"/> (<xsl:value-of select="CURRENCY"/>)
			plus <xsl:value-of select="SHIPMENT_COST"/> S/H.
	  </DIV>
	</xsl:template>
</xsl:stylesheet>

Open in new window

Avatar of Pete_Zed

ASKER

Unfortunately the code you supplied resulted in a blank page. No data was returned either on screen or in the source code. No error messages either.

See code snippet: The first code is my index.asp page and the second is the XSL file.
<% option explicit %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
 
<%
dim objXSL 
dim objXML
Dim xslt
Dim xslProc
 
set objXML = Server.CreateObject("MSXML2.DOMDocument")
if not objXML.load("http://www.fishpond.co.nz/Movies/Blu_ray/Animated/xml.php?ref=1006") then
        Response.Write "Error loading data"
        Response.Write "<br>Error number: " & err.number 
        Response.Write "<br>Description: " & err.Description
        Response.end
end if
 
'create XSL template
set xslt = server.CreateObject("Msxml2.XSLTemplate")
 
set objXSL = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
if objXSL.load(Server.MapPath("Feed2XHTML.xsl")) then
        xslt.stylesheet = objXSL
else
        Response.Write "Error loading template"
        Response.Write "<br>Error number: " & err.number 
        Response.Write "<br>Description: " & err.Description
        Response.end
end if
 
'Transform to XHTML
Set xslProc = xslt.createProcessor()
xslProc.input = objXML
xslProc.Transform
Response.ContentType = "text/xhtml"
Response.Write xslproc.output
 
set objxml = nothing
set objxsl = nothing
%>
</body>
</html>
 
=================================
 
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html" encoding="iso-8859-1"/>
	<xsl:template match="/">
		<xsl:apply-templates select="STORE/PRODUCTS/PRODUCT"/>
	</xsl:template>
		
	<xsl:template match="PRODUCT">
	   <DIV style="clear: both; margin-top:2em; border-top: solid 1px red;">
		<IMG src="{IMAGE}" align="left"/>
		<A href="{PRODUCT_URL}"><xsl:value-of select="ATTRIBUTE1"/></A>
		<xsl:if test="string-length(author)">
			by <xsl:value-of select="author"/>
  		</xsl:if>
		<BR/>ISBN: <xsl:value-of select="isbn"/>
		<BR/>Price: <xsl:value-of select="PRICE"/> (<xsl:value-of select="CURRENCY"/>)
			plus <xsl:value-of select="SHIPMENT_COST"/> S/H.
	  </DIV>
	</xsl:template>
</xsl:stylesheet>

Open in new window

try to add objXML.async = false  and objXSL.async = false
before each corresponded load() call
Your XML feed site does not respond, so I'm unable to verify is the code work.
I added the code as suggested but now I get this:

Error loading data
Error number: 0
Description:

I checked and I can get to the XML feed from my end.
I tried to run it again, it works.
It strange, you've got false as a return of the load() function an the error number is 0.
What version of MSXML do you have? Try to install the latest.

Also, try not to check the return value of the load(), let the execution go further.
And you can try to check the parsing error code, like follows:
objXML.async = false
objXML.load "http://www.fishpond.co.nz/Movies/Blu_ray/Animated/xml.php?ref=1006"
If objXML.parseError.errorCode <> 0 Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   Response.Write "You have error " & myErr.reason
End If 

Open in new window

I tried with the parsing code but it returns this:

You have error No data is available for the requested resource.

I have checked and reinstalled MSXLM6. I also tried without error checking and it displays a blank screen. Hmmm.
Well, after much research I found that the code below works great and retrieves the information every time.
<%
'Initialise variables
Dim oXMLHTTP
Dim oResponseXML
Dim bXMLLoadError
 
Set oXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP") 'Create instance of the Microsoft XMLHTTP object
 
'Use the Microsoft XMLHTTP object to retrieve the XML from the remote server
oXMLHTTP.Open "GET", "http://www.fishpond.co.nz/Movies/Blu_ray/Family/xml.php?ref=1006", false
oXMLHTTP.Send("")
 
'Check that the XML was retrieved successfully by checking to ensure that a HTTP status code of 200 was received (200 = OK)
If oXMLHTTP.Status = 200 Then
    Set oResponseXML = oXMLHTTP.ResponseXML 'Save XML to an object
    bXMLLoadError = False
Else
    Response.Write("<font color=""red"">Page Error: Could not load XML from remote server</font><br>") 'Show error message
    bXMLLoadError = True
End If
 
If Not bXMLLoadError Then
 
    'Load the XML from the oResponseXML object
	Dim oXML
	Dim oXSL
    set oXML = Server.CreateObject("Microsoft.XMLDOM")
    oXML.Async = false
    oXML.Load(oResponseXML)
 
    'Load the XSL from disk
    set oXSL = Server.CreateObject("Microsoft.XMLDOM")
    oXSL.Async = False
    oXSL.Load(Server.MapPath("Feed2XHTML.xsl"))
 
    Response.Write(oXML.transformNode(oXSL)) 'Transform the XML using the XSL stylesheet
 
End If
Set oXMLHTTP = nothing 'Release Microsoft XMLHTTP object
%>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of zc2
zc2
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account