from https://code.google.com/p/xqpretty (Apache License 2.0)