1
votes

I'm working on an old software written in classic ASP (VBScript).

I should create an asp page that makes an XSL transformation. I'm able to do this using static files but I have to work with XML files dynamically generated by asp pages (this doesn't work).

This is my code:

Dim document, stylesheet, o
Set document = Server.CreateObject("Msxml2.DOMDocument")
document.async = False

Set o = CreateObject("MSXML2.XMLHTTP")
o.open "GET", "http://localhost/aaa/cgi-bin/fo/file.asp?id_x=39", False
o.send

'document.load Server.MapPath("test.xml") ' <- with static file is working
'document.loadXML(o.responseText) ' <- not working
document.load o.responseXML ' <- not working

Set stylesheet = Server.CreateObject("Msxml2.DOMDocument")
stylesheet.async = False
stylesheet.load Server.MapPath("test.xslt")

'Response.Write o.responseText ' <- working! (return the correct XML)
'Response.Write o.responseXML.xml ' <- not working (empty result)
Response.Write document.transformNode(stylesheet)

Set document = Nothing
Set stylesheet = Nothing

All is running on a virtual machine with Windows 2000 Server (unfortunately I need to do this in this way).

Thank you for your help.

3

3 Answers

1
votes

Lankymart's answer will probably work, but here's a tried and tested example I've used before with the Bing API

<%
    Option Explicit
    Dim xml, xsl    

    Set xml = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0")
    xml.open "GET","http://www.yourxmlsource.com", false, "myUid", "myPassword"
    xml.send 
    Response.ContentType = "application/xml"

    Set xsl = Server.CreateObject("Msxml2.DomDocument.6.0")
    xsl.load(Server.Mappath("yourxslfile.xsl"))

    response.write xml.responseXML.transformNode(xsl)

    Set xsl = Nothing
    Set xml = Nothing
%>

If you don't need to send login information to get your XML then you may not actually need to use get. Here's what I use for RSS feeds

<% 
  dim xml, xsl
  set xml = Server.CreateObject("Msxml2.DomDocument.6.0")
  xml.setProperty "ServerHTTPRequest", true
  xml.async = false
  xml.validateOnParse = false
  xml.load("http://rssurl.com")

  set xsl = Server.CreateObject("Msxml2.DomDocument.6.0")
  xsl.load(Server.Mappath("rss.xsl"))
  Response.Write(xml.transformNode(xsl))
  set xsl = nothing
  set xml = nothing 
%>
1
votes

This is a quick stab (untested).

Based on what you already have, I've made minor changes. You don't need to declare your Msxml2.DOMDocument object, by using Set document = o.responseXML you get the same effect. Added a HTTP Status code check to capture any issues.

Looking at this line;

document.load o.responseXML

This will not work because the Load() method expects a file location.

Dim document, stylesheet, o

'Should be using the IServerXMLHTTPRequest object
Set o = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
With o
  Call .Open("GET", "http://localhost/aaa/cgi-bin/fo/file.asp?id_x=39", False)
  Call .setRequestHeader("Content-Type", "application/xml; charset=utf-8")
  Call .Send()
  If .Status = 200 Then
     Set document = o.responseXML
  Else
    'Handle errors
  End If
End With

Set stylesheet = Server.CreateObject("Msxml2.DOMDocument")
stylesheet.async = False
stylesheet.load Server.MapPath("test.xslt")

Response.Write document.transformNode(stylesheet)

Set document = Nothing
Set stylesheet = Nothing
0
votes

I solved the problem. It was a tricky error.

Asp reads from a database in which the chars are encoded using ISO-8859-1.

The generated XML contained an attribute with an accented character (name="unità"). When I loaded the page my responseText contained name="unit? (the last " was surprisingly cutted off). So I was working with a malformed XML document very similar to the correct document.

For this reason it printed the responseText but not the responseXML.

I solved it changing database values directly, using url encoding for special chars.