[lxml-dev] schema validation and resolvers

Stefan Behnel stefan_ml at behnel.de
Tue Jun 24 09:36:37 CEST 2008


Hi,

Michael Ballbach wrote:
> I've been trying to use etree.XMLSchema() to load a schema that has
> external imports - but I'd like to utilize an etree.Resolver object. In
> my application schemas aren't always on disk.
> 
> This does not work, because the libxml2 xmlSchemaAddSchemaDoc()
> function, when called to prepare the import, creates a new parser
> context and the _local_resolver() function is unable to map this unknown
> context to a Resolvers object.

I agree that this is not satisfactory. It means that a quirk in libxml2 leaks
into Python space.


>  2) Use the _ParserDictionaryContext system there to store state about
>     when the schema code is entered so that a proper XML context can be
>     inferred and the original document's parser's resolvers can be
>     called. One must be careful here to make sure that any use of lxml
>     within the resolver callbacks does not mess up this state.

I think that's the way to go. And yes, it must be a stack (i.e. a list) in
this case. I'd just push the right parser (not only its context) as a "current
thread parser" in cases where we know that libxml2 will not provide us with a
context (XInclude is another case, happy to see that resolved at the same
time). Then we can fall back to using the top-most parser if the _private
pointer is NULL.

Stefan




More information about the lxml-dev mailing list