You've probably already read this, but it's a very elegant algorithm for validating Relax NG. http://www.thaiopensource.com/relaxng/derivative.html