RE: [xsl] Simple XML to XML transformation question

Subject: RE: [xsl] Simple XML to XML transformation question
From: "Joe Fawcett" <joefawcett@xxxxxxxxxxx>
Date: Thu, 18 Nov 2004 14:32:02 +0000
From: "Joe Heidenreich" <HeidenreichJ@xxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: RE: [xsl] Simple XML to XML transformation question
Date: Thu, 18 Nov 2004 09:07:54 -0500

David Wrote:
>   view source in IE always shows the original source not the transform.


I was sort of thrown into the middle of learning XSLT from a pre-existing project. They handled their transforms like this:


 Dim sourceFile, styleFile, source
 'sourceFile = Server.GetFile()
 styleFile = Server.MapPath("style.xsl")

 'Load the XML
 set source = Server.CreateObject("MSXML2.DOMDocument")
 source.async = false
 source.load(path)

 ' Load the XSL
 set  style = Server.CreateObject("MSXML2.DOMDocument")
 style.async = false
 style.load styleFile

 result = source.transformNode(style)
 Response.Write(result)

Which produces the transform in the browser, and by viewing the source you do see the transformed file. Even if the output method is XML, when the document is translated you will not see unknown elements in the browser, but they would show up in the source. Is this a bad practice? Is there a better way to be handling this?

I have since edited this code so that my output is written to a file instead of to a browser, but it would be a way to have the view source show the transform.


There are a few improvements that could be made.
Firstly specify what class version to use, change:
"MSXML2.DOMDocument"
to
"MSXML2.DOMDocument.4.0"
if you have version 4, the latest, otherwise use
"MSXML2.DOMDocument.3.0".
Otherwise chnages on the server external to your app can change which parser you get


Secondly it is more efficient to clone the second instance:

set  style = source.cloneNode(false)
'Next line not needed as second instance inherits first's
'style.async = false
'Next line is not needed with version 4 parser.
style.setProperty "SelectionLanguage", "XPath"
'style.load styleFile

Thirdly never do response.write if the whole page is produced via a transform, do:

source.transformNodeToObject(style, Response)

This will keep encodings as specified in stylesheet, otherwise you always get UTF-16.

Fourthly you should set the content type to xml if the output is xml, then it will show in the browser:

Response.ContentType = "text/xml"

--

Joe

Current Thread