#!/usr/bin/python -u import sys import string import libxml2 import libxslt nodeName = None def f(ctx, str): global nodeName # # Small check to verify the context is correcly accessed # try: pctxt = libxslt.xpathParserContext(_obj=ctx) ctxt = pctxt.context() tctxt = ctxt.transformContext() nodeName = tctxt.insertNode().name except: pass return string.upper(str) libxslt.registerExtModuleFunction("foo", "http://example.com/foo", f) styledoc = libxml2.parseDoc(""" failure
""") style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseDoc("") result = style.applyStylesheet(doc, { "bar": "'success'" }) style.freeStylesheet() doc.freeDoc() root = result.children if root.name != "article": print "Unexpected root node name" sys.exit(1) if root.content != "SUCCESS": print "Unexpected root node content, extension function failed" sys.exit(1) print root.content if nodeName != 'article': print "The function callback failed to access its context" sys.exit(1) result.freeDoc()