本文介绍了LXML和XSL document()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了以下文件:
merge.py:
from lxml import etree
xml_input = etree.XML(open('a.xml', 'r').read())
xslt_root = etree.XML(open('merge.xsl', 'r').read())
transform = etree.XSLT(xslt_root)
print str(transform(xml_input))
merge.xsl:
merge.xsl:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<merge>
<xsl:copy-of select="."/>
<xsl:copy-of select="document('b.xml')"/>
</merge>
</xsl:template>
a.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<a>
<test id="1"/>
</a>
b.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<b>
<test id="2"/>
</b>
当我调用merge.py时,这4个文件在同一目录中:
The 4 files are in the same directory, when i call merge.py i got the error:
lxml.etree.XSLTApplyError: Cannot resolve URI string://__STRING__XSLT__/b.xml
有什么主意吗?我做错了什么?
Any idea ? on what i do wrong ?
推荐答案
您需要注册URI解析器.请参见文档.
You need to register a URI resolver. See the documentation.
大概是这样(未经测试):
Probably something like this (untested):
class FileResolver(etree.Resolver):
def resolve(self, url, pubid, context):
return self.resolve_filename(url, context)
parser = etree.XMLParser()
parser.resolvers.add(FileResolver())
xml_input = etree.parse(open('a.xml','r'), parser)
xslt_root = etree.parse(open('merge.xsl','r'), parser)
transform = etree.XSLT(xslt_root)
print str(transform(xml_input))
这篇关于LXML和XSL document()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!