Ruby, libxml and Windows

Let’s say my last post tempted you to check out libxml’s validation functionality. However, you work in shop that develops on multiple platforms, including Windows, OS X and Linux, so you code has to run on all three platforms.

You happily note that libxml does run on all three platforms, so you download and install it. Next, you fire up your friendly command prompt and ask Ruby Gems to install the appropriate bindings.

Unfortunately, you discover there isn’t a pre-built binary for Windows. Being in a hurry, you open up VC++ (experience has taught you that MinGW is a royal pain and will undoubtedly eat up the rest of your day).

Sadly, VC++ doesn’t compile the extension. At least with this code, its more pedantic than GCC and flags some sketchy code that increments void pointers. You also note that you have to export the main extension function from the DLL (what developer at Microsoft years ago thought this was a good idea?).

So, to save you the trouble, I’ve provided a binary that works with Ruby 1.8.5. If you’d like to see the patch, its available at RubyForge.

To use the extesion, copy it into this directory:

ruby\lib\ruby\site_ruby\1.8\i386-msvcrt\xml

Then write code that looks something like this:

require 'xml/libxml_so'

def verify(html)
  dtd = XML::Dtd.new("public", 'xhtml1-transitional.dtd')
  parse =  XML::Parser.string(html)
  parse.validate(dtd)
end

Yup, the “_so” bit is required due to the way the extension is packaged (it shouldn’t be, and I was tempted to remove it, but thought it was better to maintain cross-platform compatibility).

Happy hacking!

Leave a Reply

Your email address will not be published. Required fields are marked *

Top