Kay lives here

working with the web

Validator to RSS in CF

This is way cool: Ben Hammersley’s XHTML Validator to RSS tool.

Here’s how it works – the W3C HTML Validator will actually return results in XML. Ben’s Perl script (source code provided) returns the results as an RSS feed if there are any errors in the HTML (nothing is returned if the page is clean). Which is a great and sneaky way to keep an eye on content managed sites or sites that are maintained by users who couldn’t care less about web standards. We all have those.

So, I thought it would be fun to convert the code to ColdFusion. I haven’t had a need to use CF’s XML parsing functions, and figured now is as good a time as any.

Try it out on this site:
http://kay.smoljak.com/validate.cfm?uri=http://kay.smoljak.com

(it returns the RSS as application/xml+rss mimetype so you won’t be able to see it in a browser).

Here’s the code:


<cfhttp url="http://validator.w3.org/check?uri=#url.uri#;output=xml"></cfhttp>
<cfset validator = xmlparse(cfhttp.filecontent)>
<cfsavecontent variable="output"><cfoutput><?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>#xmlformat("XHTML Validation results for #url.uri#")#</title>
<link>#xmlformat("http://validator.w3.org/check?uri=#url.uri#")#</link>
<description></description>
<cfif StructKeyExists(validator.result,"messages")>
<cfloop from="1" to="#arraylen(validator.result.messages[1].msg)#" index="i">
<item>
<title>#xmlformat("Line:#validator.result.messages[1].msg[i].xmlattributes.line#           Column:#validator.result.messages[1].msg[i].xmlattributes.col#")#</title>
<link>#xmlformat("http://validator.w3.org/check?uri=#url.uri#")#</link>
<description>#xmlformat("#validator.result.messages[1].msg[i].xmltext#")#</description>
</item>
</cfloop>
</cfif>
</channel>
</rss>
</cfoutput></cfsavecontent>
<cfcontent type="application/rss+xml"><cfoutput>#output#</cfoutput>

If you wanna see the output in a browser, change the content-type to text/plain.

Like I said, it’s my first play with CF’s XML parsing functions, so if there’s a better way to do something, please let me know!

Comments are closed.