Q:

LXML and XSL document() Function

Hi i got the following files :
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:
<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>   

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 ?

A:

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))

A:

Why not simply do this?
from lxml import etree

xml_input = etree.parse("a.xml")
xslt_root = etree.parse("merge.xsl")
transform = etree.XSLT(xslt_root)
print str(transform(xml_input))

More straightforward, no need for a resolver.

