Partager via


Transforming XML Content in IIS

This topic describes how to transform an XML file on an IIS server before sending the formatted content to a client. The transformation uses an XSLT file.

Example Code

The following example shows you how to use JScript to to fill an HTML DIV tag with the result of a transformation performed by a stylesheet called sample.xsl on a file called books.xml. This code can be placed in an HTML file.

This example was taken from the topic title transformNode.

<SCRIPT> 
   // Load data. 
   var source = new ActiveXObject("Msxml2.DOMDocument.4.0"); 
   source.async = false; 
   source.load("books.xml"); 
if (xmlDoc.parseError.errorCode <> 0) { 
   var myErr = xmlDoc.parseError; 
   alert("You have error " + myErr.reason); 
} else { 
   // Load style sheet. 
   var stylesheet = new ActiveXObject("Msxml2.DOMDocument.4.0"); 
if (xmlDoc.parseError.errorCode <> 0) { 
   var myErr = xmlDoc.parseError; 
   alert("You have error " + myErr.reason); 
} else { 
      stylesheet.async = false 
      stylesheet.load("sample.xsl"); 

      // Fill a div tag with the result of the transform 
      divInfo.innerHTML = source.transformNode(stylesheet); 
   } 
} 
</SCRIPT> 

Example Code

The following example shows you how to use Visual Basic Scripting Edition (VBScript) to display the result of a transformation that is performed by a stylesheet called sample.xsl on a file whose name is sent in the Request.QueryString collection. This code can be placed in an ASP file.

Wrapping the call to Request.QueryString is a basic method of protecting against malicious user input. A more robust method is used in Validating User Input to Avoid Attacks.

This example was altered from sample in the the topic title transformNode.

<%@ Language = "VBScript" %> 

<% 
Dim source As New Msxml2.DOMDocument40 
Dim stylesheet As New Msxml2.DOMDocument40 

' Load data. 
source.async = False 
source.Load Server.HTMLEncode(Request.QueryString("XMLFileName")) 
If (xmlDoc.parseError.errorCode <> 0) Then 
    Dim myErr 
    Set myErr = xmlDoc.parseError 
    Response.Write("You have error " & myErr.reason) 
Else 
    ' Load style sheet. 
    stylesheet.async = False 
    stylesheet.Load "sample.xsl" 
    If (xmlDoc.parseError.errorCode <> 0) Then 
        Dim myErr 
        Set myErr = xmlDoc.parseError 
        Response.Write("You have error " & myErr.reason) 
    Else 
        ' Do the transform. 
        Response.Write(source.transformNode(stylesheet)) 
    End If 
End If 
%>