1 #!/usr/bin/env python 2 3 """ 4 XSL-based form templating. 5 """ 6 7 import libxslt 8 9 """ 10 import libxml2 11 12 def quiet(context, s): 13 pass 14 15 libxml2.registerErrorHandler(quiet, None) 16 """ 17 18 def path_to_node(node): 19 20 "Generate an XSLForms path to the given 'node'." 21 22 l = [] 23 # Attribute reference. 24 l.insert(0, node.name) 25 l.insert(0, "/") 26 node = node.parent 27 # Element references. 28 while node is not None and node.type != "document_xml": 29 l.insert(0, str(int(node.xpathEval("count(preceding-sibling::*) + 1")))) 30 l.insert(0, "#") 31 l.insert(0, node.name) 32 l.insert(0, "/") 33 node = node.parent 34 return "".join(l) 35 36 def this_position(context): 37 38 """ 39 As a libxslt extension function, return a string containing the XSLForms 40 path to the 'context' node. 41 """ 42 43 pctxt = libxslt.xpathParserContext(_obj=context) 44 context = pctxt.context() 45 node = context.contextNode() 46 return path_to_node(node) 47 48 libxslt.registerExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_position) 49 50 # vim: tabstop=4 expandtab shiftwidth=4