1#!/usr/bin/python -u 2import sys 3import string 4import libxml2 5# Memory debug specific 6libxml2.debugMemory(1) 7import libxslt 8 9nodeName = None 10 11def f(ctx, str): 12 global nodeName 13 14 # 15 # Small check to verify the context is correcly accessed 16 # 17 try: 18 pctxt = libxslt.xpathParserContext(_obj=ctx) 19 ctxt = pctxt.context() 20 tctxt = ctxt.transformContext() 21 nodeName = tctxt.insertNode().name 22 except: 23 pass 24 25 return string.upper(str) 26 27libxslt.registerExtModuleFunction("foo", "http://example.com/foo", f) 28 29styledoc = libxml2.parseDoc(""" 30<xsl:stylesheet version='1.0' 31 xmlns:xsl='http://www.w3.org/1999/XSL/Transform' 32 xmlns:foo='http://example.com/foo' 33 exclude-result-prefixes='foo'> 34 35 <xsl:param name='bar'>failure</xsl:param> 36 <xsl:template match='/'> 37 <article><xsl:value-of select='foo:foo($bar)'/></article> 38 </xsl:template> 39</xsl:stylesheet> 40""") 41style = libxslt.parseStylesheetDoc(styledoc) 42doc = libxml2.parseDoc("<doc/>") 43result = style.applyStylesheet(doc, { "bar": "'success'" }) 44style.freeStylesheet() 45doc.freeDoc() 46 47root = result.children 48if root.name != "article": 49 print "Unexpected root node name" 50 sys.exit(1) 51if root.content != "SUCCESS": 52 print "Unexpected root node content, extension function failed" 53 sys.exit(1) 54if nodeName != 'article': 55 print "The function callback failed to access its context" 56 sys.exit(1) 57 58result.freeDoc() 59 60# Memory debug specific 61libxslt.cleanup() 62if libxml2.debugMemory(1) == 0: 63 print "OK" 64else: 65 print "Memory leak %d bytes" % (libxml2.debugMemory(1)) 66 libxml2.dumpMemory() 67 sys.exit(255) 68