Re: [lxml-dev] ElementInclude doesn't honour base URLs
Mike Meyer wrote:
In <4679A865.3010706@behnel.de>, Stefan Behnel <stefan_ml@behnel.de> typed:
Mike Meyer wrote:
The problem is that using ElementInclude.include doesn't resolve href attributes relative to their containing document. Fixed on the trunk and for 1.3.
Cool. Thank you.
For the archives, my workaround for 1.2.x is:
from os.path import join, split from lxml.etree import parse from lxml.ElementInclude import include, default_loader
class Loader(object): """Custom loader for XInclude processing to fix relative addressing for files."""
def __init__(self, name): self.base = split(name)[0]
def __call__(self, href, typ, encoding=None): return default_loader(join(self.base, href), typ, encoding)
This only works for local files. You can use "urlparse.urljoin" to make it work for more or less any file path or URL (remember that lxml supports ftp and http). You don't even need to do the split before hand, BTW. Ah, and I don't think this works for included files including files themselves from different directories. But it's still a good work around for most use cases. Stefan
participants (1)
-
Stefan Behnel