Libxml2 exports an API to handle DTDs and validation, check the associated description. So there may be a number of examples on-line, I will just list one for now, others pointers welcome: I suggest looking at the examples found under test/valid/dtd and any of the large number of books available on XML.
The dia example in test/valid should be both simple and complete enough to allow you to build your own.
XPath object (see XPath and XSLT with lxml: Stylesheet-parameters for background on this).
The The usage of validation phases is a unique feature of ISO-Schematron and can be a very powerful tool e.g.
The DTD is retrieved automatically based on the DOCTYPE of the parsed document.