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 # Skip attribute reference. 24 if node.type == "attribute": 25 node = node.parent 26 # Element references. 27 while node is not None and node.type != "document_xml": 28 l.insert(0, str(int(node.xpathEval("count(preceding-sibling::*) + 1")))) 29 l.insert(0, "#") 30 l.insert(0, node.name) 31 l.insert(0, "/") 32 node = node.parent 33 return "".join(l) 34 35 def this_position(context): 36 37 """ 38 As a libxslt extension function, return a string containing the XSLForms 39 path to the 'context' node. 40 """ 41 42 pctxt = libxslt.xpathParserContext(_obj=context) 43 context = pctxt.context() 44 node = context.contextNode() 45 return path_to_node(node) 46 47 libxslt.registerExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_position) 48 49 # vim: tabstop=4 expandtab shiftwidth=4