When working in Docbook you sometimes have to debug errors with very little help from the XML parser or build tools. The simplest way to do this is by process of elimination: you comment out large chunks of the book to try to isolate the error. Not rocket science, just tedious. The major issue you encounter doing this (especially in very large books) is that there might be <xref>s from one part of the book into a commented part, and so you have to hunt these down and comment them out as well. It’s a major pain.

So, regular expressions to the rescue…

These are two commands using sed and grep to change all the <xref>s in a directory of XML files to comments, and then back again.


  sed -i -r -e 's/<(xref linkend.*\/)>/<!-- \1 -->/g' `grep -Rl '<xref linkend.*/>' .
  --exclude-dir='tmp' --exclude-dir='.svn'`

This command finds all the <xref> tags in XML files in the current directory and sub-directories and comments them out.

And back again

  sed -i -r -e 's/<!-- (xref linkend.*\/) -->/<\1>/g'  `grep -Rl '<!-- xref linkend.*/ -->' .
  --exclude-dir='tmp' --exclude-dir='.svn'`

This command finds all the <xref> comments and turns them back into proper <xref> tags again.

And since you are using source control you can easily see what changes are made. You are using source control aren’t you?

Easy. You might want to create aliases for these … an exercise for the student. :-)