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