An XSLT for Furl XML
Jul 20th, 2004 by Karen
An XSLT for Furl XML
A while back I mentioned the fact that I was using XML from Furl to drive the “Resources” portion of my site. I decided that since other people might want to do this and don’t know how I should probably post the code. To do this transform you need a couple of things. First, an XML parser and some sort of server-side scripting language are necessary. I'm using MSXML and ASP because my site lives on a Microsoft box. You can also use another parser and scripting language. I believe the latest version of PHP has an XML parser built-in. Once you have this there are two pieces of code you need to write. The first is the XSLT file that transforms your XML document into XHTML/HTML. Below is the code in my XSLT file. Because this is an XSLT file, it is platform independent.
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:fo=”http://www.w3.org/1999/XSL/Format”>
<xsl:output method=”html”/>
<xsl:param name=”topic”/>
<xsl:key name=”list” match=”entry” use=”topic” />
<xsl:template match=”/”>
<xsl:for-each select=”/archive/entry[generate-id(.)=generate-id(key('list',topic))]/topic”>
<!– Only process the item if it's
the first time we've seen the Name –>
<xsl:sort order=”ascending” select=”.” />
<xsl:if test=”contains(translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),translate($topic,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))”>
<xsl:element name=”h3″>
<xsl:value-of select=”.”/>
</xsl:element>
<xsl:for-each select=”key('list',.)”>
<xsl:element name=”h4″>
<xsl:element name=”a”>
<xsl:attribute name=”href”>
<xsl:value-of select=”@uri”/>
</xsl:attribute>
<xsl:value-of select=”@title”/>
</xsl:element>
</xsl:element>
<xsl:element name=”p”>
<xsl:value-of select=”description”/>
</xsl:element>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The second bit of code is the server-side scripting code that takes the XSLT file and the XML file, send them to the parser and transforms them on the fly. As I said before I'm using ASP to do this. Below is the ASP code that performs the task of bring together the XML file and XSL and sends them to the XML parser.
<%
set xslt = Server.CreateObject(”MSXML2.XSLTemplate.4.0″)
set xslDoc = Server.CreateObject(”Msxml2.FreeThreadedDOMDocument.4.0″)
set xmlDoc = Server.CreateObject(”Msxml2.DOMDocument.4.0″)
Dim xslProc
xslDoc.async = false
xslDoc.resolveExternals = false
xslDocName = “all_links.xsl”
xslParam = topic
xslDoc.load (Server.MapPath(xslDocName))
xslDoc.validateOnParse = False
set xslt.stylesheet = xslDoc
xmlDoc.async = false
xmlDoc.resolveExternals = false
xmlDoc.load (Server.MapPath(”exportXML.xml”))
set xslProc = xslt.createProcessor()
xslProc.input = xmlDoc
xslProc.addParameter “topic”, xslParam
xslProc.transform()
response.write xslProc.output
%>
Using these two bits of code I transform the XML version of my Furl archive into the resource pages on this site. Overall, it a pretty easy but power trick that helps me multi-purpose content I've in a variety of ways.

