[xml] python wrapper for xslt document API: context problem; __cmp__ needed?



My reason for building the python wrapper for the xslt document API is
so that a single python document handling function can deal behave
differently for different stylesheet applications. For example, it
might serve import files from user specific directories.

The doc loader API defines a context to be passed to the document
loader, which is said to be the contextual stylesheet. So when the
document handler function is called it has a reference to the
stylesheet that has caused the handler function to be called.


So I have code like this in my C wrapper for the python doc loader
function:

    if (pythonDocLoaderObject != NULL) {
        xsltStylesheetPtr style;
        PyObject *ctxtobj;
        PyObject *pctxtobj;
        PyObject *result;

        style = (xsltStylesheetPtr) ctxt;
        ctxtobj = libxslt_xsltStylesheetPtrWrap(ctxt);
        pctxtobj = libxml_xmlParserCtxtPtrWrap(pctxt);

        // For now just send parserContext and URL
        result = PyObject_CallFunction(pythonDocLoaderObject, 
                                       (char *) "(sOO)", URI, pctxtobj, ctxtobj);
        Py_XDECREF(pctxtobj);


And the python looks like this:

def fn(url, pctx, ctx):
    try:
        # print url
        style = libxslt.stylesheet(_obj=ctx)
        print "inside the function: %s" % (style)
        pctxt = libxml2.parserCtxt(_obj=pctx)
        doc = pctxt.ctxtReadDoc("""<?xml version='1.0'?><b>Goodbye</b>""", url, "UTF-8", 2)
        # doc.dump(sys.stdout)
        return doc
    except Exception, e:
        print >>sys.stderr, "something went wrong: ", e
        return None


def run():
    libxslt.setLoaderFunc(fn)
    styledoc = libxml2.parseDoc("""
    <xsl:stylesheet version='1.0'
      xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
      <xsl:template match='/'>
        <x><xsl:value-of select='document("b.xml")'/></x>
      </xsl:template>
    </xsl:stylesheet>
    """)
    style = libxslt.parseStylesheetDoc(styledoc)
    print "outside the function: %s" % (style)
    doc = libxml2.parseDoc("<a/>")
    result = style.applyStylesheet(doc, {})
    result.dump(sys.stdout)
    style.freeStylesheet()
    doc.freeDoc()


The trouble is the xslt is wrapped by the python and the python
objects are NOT the same (obvious really, since they're being
wrapped).

So I think I probably need the stylesheet to implement the Python
comparator interface. 

A naive implementation would be in C and would test that the libxslt
stylesheet struct was the same instance. That would do for me.


Does anyone have any thoughts on this? Is there a simpler way to
achieve what I want?

-- 
Nic Ferrier
http://www.tapsellferrier.co.uk   for all your tapsell ferrier needs



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]