Posted on by & filed under epub, tools.

Most of the time, command-line users will be able to run the epubcheck Java binary without issues. But sometimes mysterious problems occur — this happened to me on OS X before upgrading to Snow Leopard. So while it’s a bit awkward, you can use the remote epubcheck API combined with some XSLT to get the same effect as running epubcheck locally.

As in the earlier post about bash ePub tools, let’s create a shortcut:

function epubcheck()
curl --silent --data-binary @$@

Parsing the validator output

The validator will just return raw XML to the console; if you wanted to produce more human-readable output, use this XSLT:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="" 
  <xsl:output method="text" media-type="text/plain" encoding="UTF-8"></xsl:output>
  <xsl:template match="/">
      <xsl:when test="//is-valid='False'">
        <xsl:text>File was NOT valid!</xsl:text>
        <xsl:for-each select="//error">
          <xsl:value-of select="."/><xsl:text>&#xa;</xsl:text>            
        <xsl:text>File was valid!</xsl:text>

Save that as epubcheck.xsl then refactor the function to capture the output from the API and run it through the XSLT:

function epubcheck()
curl --silent --data-binary @$@ > /tmp/epubcheck;
xsltproc epubcheck.xsl /tmp/epubcheck;
rm /tmp/epubcheck

Now you’ll get an interaction that’s easy to read:

$ epubcheck code.epub
File was valid!
$ epubcheck malformed.epub 
File was NOT valid!
ERROR: temp.epub: OPF file OEBPS/content.opf is missing
Check finished with warnings or errors!

But try to get epubcheck running locally first, as you’ll get better performance and save me some bandwidth.


Comments are closed.