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@16 | 7 | import Constants |
paulb@1 | 8 | import libxslt |
paulb@1 | 9 | |
paulb@1 | 10 | """ |
paulb@1 | 11 | import libxml2 |
paulb@1 | 12 | |
paulb@1 | 13 | def quiet(context, s): |
paulb@1 | 14 | pass |
paulb@1 | 15 | |
paulb@1 | 16 | libxml2.registerErrorHandler(quiet, None) |
paulb@1 | 17 | """ |
paulb@1 | 18 | |
paulb@16 | 19 | def path_to_node(node, attribute_ref, name): |
paulb@1 | 20 | |
paulb@9 | 21 | """ |
paulb@16 | 22 | Generate an XSLForms path to the given 'node', producing an attribute |
paulb@16 | 23 | reference if 'attribute_ref' is true; for example: |
paulb@16 | 24 | |
paulb@16 | 25 | /package#1/discriminators#5/discriminator#1/category |
paulb@16 | 26 | |
paulb@16 | 27 | Otherwise an element reference is produced; for example: |
paulb@16 | 28 | |
paulb@16 | 29 | /package#1/discriminators#5/discriminator#1 |
paulb@16 | 30 | |
paulb@16 | 31 | Use the given 'name' to complete the path if an attribute reference is |
paulb@16 | 32 | required (and if a genuine attribute is found at the context node - |
paulb@16 | 33 | otherwise 'name' will be None and the context node will be treated like an |
paulb@16 | 34 | attribute). |
paulb@9 | 35 | """ |
paulb@1 | 36 | |
paulb@1 | 37 | l = [] |
paulb@6 | 38 | # Skip attribute reference. |
paulb@6 | 39 | if node.type == "attribute": |
paulb@6 | 40 | node = node.parent |
paulb@9 | 41 | # Manually insert the attribute name if defined. |
paulb@16 | 42 | if attribute_ref: |
paulb@16 | 43 | if name is not None: |
paulb@16 | 44 | l.insert(0, name) |
paulb@16 | 45 | l.insert(0, Constants.path_separator) |
paulb@16 | 46 | else: |
paulb@16 | 47 | l.insert(0, node.name) |
paulb@16 | 48 | l.insert(0, Constants.path_separator) |
paulb@16 | 49 | node = node.parent |
paulb@1 | 50 | # Element references. |
paulb@1 | 51 | while node is not None and node.type != "document_xml": |
paulb@1 | 52 | l.insert(0, str(int(node.xpathEval("count(preceding-sibling::*) + 1")))) |
paulb@16 | 53 | l.insert(0, Constants.pair_separator) |
paulb@1 | 54 | l.insert(0, node.name) |
paulb@16 | 55 | l.insert(0, Constants.path_separator) |
paulb@1 | 56 | node = node.parent |
paulb@1 | 57 | return "".join(l) |
paulb@1 | 58 | |
paulb@16 | 59 | def path_to_context(context, attribute_ref): |
paulb@1 | 60 | |
paulb@1 | 61 | """ |
paulb@1 | 62 | As a libxslt extension function, return a string containing the XSLForms |
paulb@9 | 63 | path to the 'context' node, using the special "this-name" variable to |
paulb@16 | 64 | complete the path if an attribute reference is required (as indicated by |
paulb@16 | 65 | 'attribute_ref' being set to true). |
paulb@1 | 66 | """ |
paulb@1 | 67 | |
paulb@1 | 68 | pctxt = libxslt.xpathParserContext(_obj=context) |
paulb@1 | 69 | context = pctxt.context() |
paulb@1 | 70 | node = context.contextNode() |
paulb@9 | 71 | transform_context = context.transformContext() |
paulb@9 | 72 | name_var = transform_context.variableLookup("this-name", None) |
paulb@9 | 73 | if name_var is not None: |
paulb@9 | 74 | name = name_var[0].content |
paulb@9 | 75 | else: |
paulb@9 | 76 | name = None |
paulb@16 | 77 | return path_to_node(node, attribute_ref, name) |
paulb@16 | 78 | |
paulb@16 | 79 | def this_position(context): |
paulb@16 | 80 | return path_to_context(context, 0) |
paulb@16 | 81 | |
paulb@16 | 82 | def field_name(context): |
paulb@16 | 83 | return path_to_context(context, 1) |
paulb@1 | 84 | |
paulb@1 | 85 | libxslt.registerExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_position) |
paulb@16 | 86 | libxslt.registerExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", field_name) |
paulb@1 | 87 | |
paulb@1 | 88 | # vim: tabstop=4 expandtab shiftwidth=4 |