Using XSLT with Active Server Pages

Using XSLT with Active Server Pages

You run Active Server Pages on Microsoft Windows NT or 2000 servers, so I use Microsofts MSXML processor on the server in this example to transform planets.xml, using planets.xsl, and return the result as an HTML document.

This is the same transformation youve now seen many times, creating an HTML table of planetary data, but this time, the transformation is done on a Web server and the user can then see that document when the server sends it. In the beginning of the ASP script, I set the MIME content-type of the result document to text/html so it will be treated as HTML:

 <%@LANGUAGE="VBScript"%>  <%      Response.ContentType = "text/html"          .          .          . 

Next, much as in the JavaScript examples earlier in this chapter, I create two MSXML document objects, one for the XML document and one for the XSL document:

 <%@LANGUAGE="VBScript"%>  <%      Response.ContentType = "text/html"      Dim docXML      Dim docXSL      Set docXML = Server.CreateObject("MSXML2.DOMDocument.3.0")      Set docXSL = Server.CreateObject("MSXML2.DOMDocument.3.0")          .          .          . 

Loading these documents on the server is much like loading them with JavaScript, except here you use the Server objects MapPath method to get the correct file paths. In this case, I place both planets.xml and planets.xsl in the same directory as the ASP script, so heres how I load these documents in:

 <%@LANGUAGE="VBScript"%>  <%      Response.ContentType = "text/html"      Dim docXML      Dim docXSL      Set docXML = Server.CreateObject("MSXML2.DOMDocument.3.0")      Set docXSL = Server.CreateObject("MSXML2.DOMDocument.3.0")      docXML.ValidateOnParse = True      docXSL.ValidateOnParse = True      docXML.load Server.MapPath("planets.xml")      docXSL.load Server.MapPath("planets.xsl")          .          .          . 

All thats left now is to use the transformNode method (as I did in JavaScript earlier) to perform the XSLT transformation and to display the results:

Listing 10.12 Server-Based XSLT Using ASP
 <%@LANGUAGE="VBScript"%>  <%      Response.ContentType = "text/html"      Dim docXML      Dim docXSL      Set docXML = Server.CreateObject("MSXML2.DOMDocument.3.0")      Set docXSL = Server.CreateObject("MSXML2.DOMDocument.3.0")      docXML.ValidateOnParse = True      docXSL.ValidateOnParse = True      docXML.load Server.MapPath("planets.xml")      docXSL.load Server.MapPath("planets.xsl")      strOutput = docXML.transformNode(docXSL)      Response.Write strOutput  %> 

You can see the result of this ASP transformation in Figure 10.5. Now youve learned now to perform XSLT transformations on Web servers.

Figure 10.5. Using XSLT with ASP.
graphics/10fig05.gif

Heres one thing to note: Now that youre performing XSLT transformations on the server, you may want to think about optimizing your transformations depending on the client, because you wouldnt necessarily want to try to create the same display in a palmtop that you would in a desktops browser. For example, you could customize the response from an ASP server script as follows , where Im checking the server variable http_user_agent , to check whether the client is using the Internet Explorer:

 <%@LANGUAGE="VBScript"%>  <%  Response.ContentType = "text/html"  If instr(request.servervariables("http_user_agent"), "MSIE") = 0 then      Response.Write "Sorry, not optimized for your device."      Response.End  End If          .          .          . 

Separating Data and Presentation

One of the strongest recent trends is to separate data and the code you use for presenting that data. This example shows why that can be a good idea: You can have one set of data and transform it on the fly for various devices.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net