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@23 | 19 | def path_to_node(node, attribute_ref, name, multivalue=0): |
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@23 | 35 | |
paulb@23 | 36 | If 'multivalue' is true, produce an attribute reference using the given |
paulb@23 | 37 | 'name' of the following form: |
paulb@23 | 38 | |
paulb@23 | 39 | /package#1/categories#1/category##name |
paulb@9 | 40 | """ |
paulb@1 | 41 | |
paulb@1 | 42 | l = [] |
paulb@6 | 43 | # Skip attribute reference. |
paulb@6 | 44 | if node.type == "attribute": |
paulb@6 | 45 | node = node.parent |
paulb@9 | 46 | # Manually insert the attribute name if defined. |
paulb@16 | 47 | if attribute_ref: |
paulb@23 | 48 | # A real attribute is referenced. |
paulb@16 | 49 | if name is not None: |
paulb@16 | 50 | l.insert(0, name) |
paulb@23 | 51 | if multivalue: |
paulb@23 | 52 | l.insert(0, Constants.pair_separator) |
paulb@23 | 53 | l.insert(0, Constants.pair_separator) |
paulb@23 | 54 | l.insert(0, node.name) |
paulb@23 | 55 | node = node.parent |
paulb@16 | 56 | l.insert(0, Constants.path_separator) |
paulb@23 | 57 | # Otherwise, treat the element name as an attribute name. |
paulb@16 | 58 | else: |
paulb@16 | 59 | l.insert(0, node.name) |
paulb@16 | 60 | l.insert(0, Constants.path_separator) |
paulb@16 | 61 | node = node.parent |
paulb@1 | 62 | # Element references. |
paulb@1 | 63 | while node is not None and node.type != "document_xml": |
paulb@1 | 64 | l.insert(0, str(int(node.xpathEval("count(preceding-sibling::*) + 1")))) |
paulb@16 | 65 | l.insert(0, Constants.pair_separator) |
paulb@1 | 66 | l.insert(0, node.name) |
paulb@16 | 67 | l.insert(0, Constants.path_separator) |
paulb@1 | 68 | node = node.parent |
paulb@1 | 69 | return "".join(l) |
paulb@1 | 70 | |
paulb@23 | 71 | def path_to_context(context, attribute_ref, multivalue_name=None): |
paulb@1 | 72 | |
paulb@1 | 73 | """ |
paulb@1 | 74 | As a libxslt extension function, return a string containing the XSLForms |
paulb@9 | 75 | path to the 'context' node, using the special "this-name" variable to |
paulb@16 | 76 | complete the path if an attribute reference is required (as indicated by |
paulb@23 | 77 | 'attribute_ref' being set to true). If 'multivalue_name' is set, produce a |
paulb@23 | 78 | reference to a multivalued field using the given string as the attribute |
paulb@23 | 79 | name. |
paulb@1 | 80 | """ |
paulb@1 | 81 | |
paulb@1 | 82 | pctxt = libxslt.xpathParserContext(_obj=context) |
paulb@1 | 83 | context = pctxt.context() |
paulb@1 | 84 | node = context.contextNode() |
paulb@9 | 85 | transform_context = context.transformContext() |
paulb@9 | 86 | name_var = transform_context.variableLookup("this-name", None) |
paulb@23 | 87 | if multivalue_name is not None: |
paulb@23 | 88 | name = multivalue_name |
paulb@23 | 89 | multivalue = 1 |
paulb@23 | 90 | elif name_var is not None: |
paulb@9 | 91 | name = name_var[0].content |
paulb@23 | 92 | multivalue = 0 |
paulb@9 | 93 | else: |
paulb@9 | 94 | name = None |
paulb@23 | 95 | multivalue = 0 |
paulb@23 | 96 | return path_to_node(node, attribute_ref, name, multivalue) |
paulb@16 | 97 | |
paulb@16 | 98 | def this_position(context): |
paulb@16 | 99 | return path_to_context(context, 0) |
paulb@16 | 100 | |
paulb@16 | 101 | def field_name(context): |
paulb@16 | 102 | return path_to_context(context, 1) |
paulb@1 | 103 | |
paulb@23 | 104 | def multi_field_name(context, multivalue_name): |
paulb@23 | 105 | return path_to_context(context, 1, multivalue_name) |
paulb@23 | 106 | |
paulb@1 | 107 | libxslt.registerExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_position) |
paulb@16 | 108 | libxslt.registerExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", field_name) |
paulb@23 | 109 | libxslt.registerExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name) |
paulb@1 | 110 | |
paulb@1 | 111 | # vim: tabstop=4 expandtab shiftwidth=4 |