View Full Version : Upload XML file to a webpage and transform it with XSL
Steerman
03-27-2008, 08:05 PM
This is my transformation page:
<%
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("test.xml"))
'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("stylesheet.xsl"))
'Transform file
Response.Write(xml.transformNode(xsl))
%>
How can i get my xml.load(Server.MapPath("test.xml")) to take any uploaded file? I want my users to put their XML documents on my site, and push "transform" and then i want stylesheet.xsl to change the page, so that it looks nice.
glenngv
04-01-2008, 02:01 AM
It's just any other file upload function. Once the file has been uploaded, you can use the above code you posted.
Google for 'Free ASP Upload'.
Steerman
04-01-2008, 07:32 AM
Hi Glen
I have tried that, but i don't know how to put them together?
Can you help me?
I have attached my upload function...
glenngv
04-01-2008, 10:20 PM
Is the upload part working? I modified the existing codes assuming the upload part is working properly.
Change the existing SaveFiles and LoadXML functions to these:
function SaveFiles
Dim Upload, Files, num, ks
Files = Array()
on error resume next
Set Upload = New FreeASPUpload
Upload.Save(uploadsDirVar)
If Err.Number<>0 then
SaveFiles = Files
Exit function
End If
on error goto 0 'reset error handling
num = Upload.UploadedFiles.Count
if (num > 0) then
Redim Files(num-1)
ks = Upload.UploadedFiles.keys
for i = 0 to num - 1
Files(i) = Upload.UploadedFiles.Item(ks(i)).FileName
next
end if
SaveFiles = Files
end function
Sub LoadXML(xml)
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(xml)
'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("stylesheet.xsl"))
'Transform file
Response.Write(xml.transformNode(xsl))
end sub
Then call the LoadXML and SaveFiles functions like this:
Dim FilesUploaded, item
FilesUploaded = SaveFiles()
if ubound(FilesUploaded) <> -1 then 'files successfully uploaded and saved
for each item in FilesUploaded
LoadXML uploadsDirVar & "\" & item
next
end if
Steerman
04-02-2008, 07:36 AM
Hi Glen
I can't seem to get it to work, what you wrote. When i put in the text you gave me, it makes an Error.
And also, the last code... where do you want me to put that?
If you can help me, i will make it worth your while...
glenngv
04-02-2008, 05:42 PM
What error are you getting?
The last code you will put in the else part when request is a POST.
<%
Dim diagnostics
if Request.ServerVariables("REQUEST_METHOD") <> "POST" then
diagnostics = TestEnvironment()
if diagnostics<>"" then
response.write "<div style=""margin-left:20; margin-top:30; margin-right:30; margin-bottom:30;"">"
response.write diagnostics
response.write "<p>After you correct this problem, reload the page."
response.write "</div>"
else
response.write "<div style=""margin-left:150"">"
OutputForm()
response.write "</div>"
end if
else
response.write "<div style=""margin-left:150"">"
OutputForm()
Dim FilesUploaded, item
FilesUploaded = SaveFiles()
if ubound(FilesUploaded) <> -1 then 'files successfully uploaded and saved
for each item in FilesUploaded
LoadXML uploadsDirVar & "\" & item
next
end if
response.write "<br><br></div>"
end if
%>
Steerman
04-02-2008, 06:52 PM
The error i get is:
variable not defined: 'xsl'
/test/test/uploadTester.asp, line 90
glenngv
04-02-2008, 07:53 PM
What's in line 90? Does the error point to one of the lines in this subroutine? I don't think there is an undefined error in here because xsl variable is defined.
Sub LoadXML(xml)
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(xml)
'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("stylesheet.xsl"))
'Transform file
Response.Write(xml.transformNode(xsl))
end sub
Steerman
04-02-2008, 08:55 PM
It still uploads the file... but then this error comes... I have attached the asp files.
Line 90 is:
set xsl = Server.CreateObject("Microsoft.XMLDOM")
This is the error page i get:
Error Type:
Der opstod en Microsoft VBScript-kørselsfejl (0x800A01F4)
Variablen er ikke defineret: 'xsl'
/test/test/uploadTester.asp, line 90
Browsertype:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
Side:
POST 10097 bytes to /test/test/uploadTester.asp
POST Data:
fejl '80020009'
Undtagelse opstod.
/iisHelp/common/500-100.asp, linje 223
Spudhead
04-03-2008, 04:38 PM
You have Option Explicit set.
Immediately above this line:
set xsl = Server.CreateObject("Microsoft.XMLDOM")
add the line:
Dim xsl
Steerman
04-03-2008, 05:42 PM
Now that code seems to work...
It uploads the file, but nothing happends after the upload.
How do i get it to show the xml (runned to the stylesheet)?
glenngv
04-03-2008, 05:57 PM
Do you get any error message?
Try linking the xsl file in the xml file and loading the xml file in the browser and see if it works. The xml file source code should look like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>
<whatever>
.
.
.
</whatever>
Change the path of the xsl as needed.
Steerman
04-03-2008, 06:20 PM
I tried putting <?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?> in the xml document, but that didn't change anything.
When i push Upload, it "reloads" the page... The upload is done, but it doesn't show any results.
glenngv
04-03-2008, 07:13 PM
What I meant was run the xml file itself (not the asp page) in the browser without the upload part. We are first trying to check if xsl transformation is working by simply linking the xsl file in the xml file. See the sample here: http://www.w3schools.com/xsl/xsl_transformation.asp
If we know that it is working, then we can go back to the xsl transformation using ASP.
Steerman
04-03-2008, 07:26 PM
Well... that works fine... I can run that without a problem.
If you want, i can send you the files, where the transformation works?
glenngv
04-03-2008, 08:06 PM
Ok, pls post or link the files here.
Steerman
04-03-2008, 08:58 PM
Here is all the files
If you run Index.asp you will see the transformation.
If you run uploadtester.asp and upload the file "test.xml" you will see that it doesn't return anything.
glenngv
04-03-2008, 09:43 PM
There's a mistake in the parameter name of the LoadXML function. The parameter for the filename of the xml file uploaded is named xml and the XMLDOM object is also named the same. So the parameter passed is overwritten by the creation of the XMLDOM object.
To solve this problem, change the LoadXML function to this:
Sub LoadXML(xmlFile)
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(xmlFile)
'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("stylesheet.xsl"))
'Transform file
Response.Write(xml.transformNode(xsl))
end sub
Steerman
04-04-2008, 07:08 AM
Hi glen
When i put this extra code in it works!!!
You are really the greatest! Couldn't have done it without you...
If there is anything you need, please let me know!
Sub LoadXML(xmlFile)
'Load XML
Dim xml
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(xmlFile)
'Load XSL
Dim xsl
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("stylesheet.xsl"))
'Transform file
Response.Write(xml.transformNode(xsl))
end sub
glenngv
04-04-2008, 08:26 AM
Oopps I forgot you have Option Explicit set. But it's always good to declare variables even without Option Explicit set. :)
Glad to help. :)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.