我有一套DTD内部调用其他DTD。所有这些文件当前都存储在我项目的主目录中。我的项目开始看起来很糟糕,所以我希望将DTD移到一个新的文件夹中。我找到了一种方法来解析主文件的DTD路径,使用像this page上的解析器一样的解析器。问题是虽然它正确地解析了主DTD的路径,但它似乎解析了内部调用文件的路径两次。由于此路径不存在,因此代码错误输出。
我的解析器:
class DTDResolver(etree.Resolver):
def resolve(self, system_url, public_id, context):
print system_url
newPath = "DTDs/" + system_url
return self.resolve_filename(newPath, context)
结果:
us-patent-grant-v40-2004-12-02.dtd
DTDs/wipo.ent
Traceback (most recent call last):
File "lxml.etree.pyx", line 3032, in lxml.etree.fromstring (src/lxml/lxml.etree.c:68121)
File "parser.pxi", line 1786, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:102470)
File "parser.pxi", line 1674, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:101299)
File "parser.pxi", line 1074, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:96481)
File "parser.pxi", line 582, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:91290)
File "parser.pxi", line 683, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:92476)
File "parser.pxi", line 622, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:91772)
lxml.etree.XMLSyntaxError: failed to load external entity "DTDs/DTDs/wipo.ent", line 237, column 7
我能够创造出一种解决方案,但它看起来非常难以理解,所以我想知道是否有更好的方法来解决问题
class DTDResolver(etree.Resolver):
def resolve(self, system_url, public_id, context):
print system_url
if system_url.startswith("DTDs/"):
return self.resolve_filename(system_url, context)
else:
newPath = "DTDs/" + system_url
return self.resolve_filename(newPath, context)