Kay lives here

working with the web

ColdFusion_icon120px-XML_icon.svg

Validator to RSS in CF

1019609_16979837

This is way cool: Ben Hammersley’s XHTML Val­ida­tor to RSS tool.

Here’s how it works — the W3C HTML Val­ida­tor will actu­ally return results in XML. Ben’s Perl script (source code pro­vided) returns the results as an RSS feed if there are any errors in the HTML (noth­ing is returned if the page is clean). Which is a great and sneaky way to keep an eye on con­tent man­aged sites or sites that are main­tained by users who couldn’t care less about web stan­dards. We all have those.

So, I thought it would be fun to con­vert the code to Cold­Fu­sion. I haven’t had a need to use CF’s XML pars­ing func­tions, and fig­ured 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 mime­type 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 out­put in a browser, change the content-type to text/plain.

Like I said, it’s my first play with CF’s XML pars­ing func­tions, so if there’s a bet­ter way to do some­thing, please let me know!

Comments are closed.