Modifying epub files to include ‘sort by’ metadata

As most people know, books are usually organised by the surname of the (first) author, alphabetically. As some others will know, ebooks are not always great at this; iTunes sometimes thinks it should sort a book by first name instead of last name, and my Nook sorts by the last word of the first author’s name regardless of where their surname is. I’ve written about my tinkering with Calibre and ebooks before and this blog post is a short snippet that builds on that. As before, I’m using Calibre to perform the tweaks.

I’ve been trying to fix this problem by including something that looks like the following in the OPF file of my epub files:

<dc:creator opf:file-as="Beagle, Peter S." opf:role="aut">Peter S. Beagle</dc:creator>

The dc:creator tag is used by the epub specification to identify the contents of the tag as a creator of the work. The opf:role="aut" is there to identify http://www.honeytraveler.com/pharmacy/ Peter S. Beagle specifically as the author of the work. opf:file-as="Beagle, Peter S." is obviously there to make it clear what the sort criterion should be for this file.

There’s only one problem with including this line: it doesn’t always work. Sometimes it does, but often my Nook will complain of an unreadable file (even though Calibre will read it just fine). So, what’s up? I have been puzzling over this issue for ages, and today I finally cracked it.

At the start of your book, there will be a <package> tag, and then below that a <metadata> tag. In order for your epub to be able to use the opf:role and opf:file-as attributes, the <metadata> tag has to look like this (or at least include this part):

<metadata xmlns:opf="http://www.idpf.org/2007/opf">

Making sure that your <metadata> tag is correct solves the problem. Wahey!