Transforming XML Server-Side
Angela and I gave a presentation to the Mid-Michigan CFUG yesterday (well actually Angela gave the presentation, and I just showed some XSL stuff), and I showed everyone how to use the new XSLT visual authoring tools in Dreamweaver 8. They're great and wonderful and everything, but they're really geared towards working with local XML files. The reason I say that, is that Macromedia doesn't offer any solutions for transforming a remote XML file. They allow you to write an XSLT file to transform that remote content, but they don't tell you how to actually pull a little Optimus Prime on it and make that XML human readable.
So, I thought I'd share some code to make this happen. It's ridiculously simply once you see it, but I've run into quite a few people that just haven't been able to put all of the pieces together. So, here's some ColdFusion code to grab MM's DesDev feed and transform it using an XSL file on your own server:
<cfsilent> <cfhttp url="http://weblogs.macromedia.com/dev_center/index.rdf" method="get" /> <cfset xmlObj = XMLParse(cfhttp.FileContent) /> <cfset xslPath = ExpandPath("mmdesdev.xsl") /> <cffile action="read" variable="xsl" file="#xslPath#" /> </cfsilent> <cfoutput>#XMLTransform(xmlObj, xsl)#</cfoutput>
Yep, believe it or not, that's all that it takes. You just read in the XML file from the server somewhere out there in internet land, convert it to an XML Object using XMLParse, and then read the XSL file from the server, and transform it all using the XMLTransform function. Can I say I love ColdFusion? Here's the same work done with ASP:
<% Dim xmlDocument, xslDocument set xmlDocument = Server.CreateObject("MSXML2.DOMDocument.4.0") set xslDocument = Server.CreateObject("MSXML2.DOMDocument.4.0") xmlDocument.async = false xmlDocument.setProperty "ServerHTTPRequest", true xmlDocument.load("http://weblogs.macromedia.com/dev_center/index.rdf") xslDocument.async = false xslDocument.load(Server.MapPath("mm.xsl")) Response.Write xmlDocument.transformNode(xslDocument.documentElement) %>
4 Comments
Daniel Short wrote on 11/09/05 2:30 PM
Well if you have Dreamweaver 8, it takes care of it all for you. Now it's more like having them lightly buffed by a nice looking young lady at the spa after an hour long massage... at least for the more simple transformations. I have quite a distaste for writing XSL files as well... DanAlexandru COSTIN wrote on 11/09/05 2:56 PM
Hi Dan, Actually, Macromedia provides you with the capability of doing server side XSL transformation - and these can be used to do XSL transformations on remote XML files. Just open the server behaviors menu and you will see the "XSL Transformation" SB in there. AlexandruDaniel Short wrote on 11/09/05 2:56 PM
DOH! Shows what I get for not looking harder. It does indeed, though the process behind the scenes seems a lot more complicated than absolutely necessary. I'll have to take a look at the classes and cfcs it adds, but thanks :). Dan
Eric Meyer wrote on 11/09/05 2:30 PM
I love how "that's all it takes" hides the fact that you have to write an XSLT file. Which is, in my experience, the XML equivalent of having one's fingernails pulled out with a pair of pliers and the newly exposed flesh abraded with #20 sandpaper. For about an hour. Not that I'm bitter.