[thelist] one more XML question...

Madhu Menon madhu at asiacontent.com
Wed Aug 9 05:08:57 CDT 2000


At 02:02 AM 8/9/00 -0500, you wrote:
>i am using IE's xslt (even though i want to use the java ones)...is there
>anyway to save the resulting HTML to a file? 'view source' and 'save' both
>do the xml, not the html...

If you want a server-side solution using ASP, the following code may be of 
some help. I use the technique for a CMS I developed recently that uses the 
MS XML parser.

OK. Here's the code you need.
I'm assuming that both your XML and XSL are in physical text files (my 
system uses MS SQL). You can tweak this code to fit your settings, of 
course. Pretty handy for testing. I've put some (OK, lots of) comments in 
that should make life a bit easier. Comments appear /before/ the code.

(WARNING: longish bit of code here because of all the comments, but then, 
comments are a Good Thing)


--- START CODE ---

<%

' Set variables for the XML and XSL files you want to use. Modify to taste.

XMLFile = "/input.xml"
XSLFile = "/templates/frontpage.xsl"

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set fso = CreateObject("Scripting.FileSystemObject")


' Open the text file containing the XML

Set strFileInput = fso.OpenTextFile(Server.MapPath(XMLFile), 
ForReading)


' Reads the contents of the XML file into a variable named strFileInput

TextContent = 
strFileInput.ReadAll

Set fso=Nothing    ' Good habit to close connection, destroy objects as 
soon as you're done with 'em.


' Instantiate the MS XML component

set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False


' OK, OK. You can also use xml.Load(Server.MapPath(XMLFile)) to read the 
XML directly from a physical file too.

xml.LoadXML(TextContent)


' Another one for reading the XSL file

set xsl = Server.CreateObject("Microsoft.XMLDOM")


' This time I read the XSL file directly cause I didn't want to display the 
XSL
' Confusing, I know. Mail me if you want an explanation.

xsl.Load(Server.MapPath(XSLFile))


' Ah the BIG line that does the trick. This converts the XML into HTML 
using the XSL that you used. Just one line. Seriously.

strGeneratedHTML = xml.TransformNode(xsl)


' And THIS is what you wanted to do; write the HTML to a text file. In this 
case, called "default.htm"

Set fso = CreateObject("Scripting.FileSystemObject")
Set HTMLFile = fso.OpenTextFile(Server.MapPath("/default.htm"), ForWriting, 
True)
HTMLFile.Write strGeneratedHTML
HTMLFile.Close

' Clean up after yourself

Set fso = Nothing

         ' Uncomment the line below if you want to spit out the XML in its 
pure form to the browser
         ' Response.Write TextContent

         ' Uncomment the line below if you want to spit out the generated 
HTML to the browser
         ' Response.Write strGeneratedHTML

         ' Voila!



%>

----- END CODE -----

That's pretty much it. Holler if you want some help. You were looking for a 
server-side solution, right?

Cheers,

Madhu



<<<   *   >>>
Madhu Menon
Webmaster, India.CNET.com
http://India.CNET.com
The source for computers and technology






More information about the thelist mailing list