# HG changeset patch # User paulb # Date 1115406617 0 # Node ID 2b1179c7ab815919fbf7b2dd26f80c2fd64a8e8a # Parent 5ac6333ec2a3191329dc7aff4bbfcf1aee8e4b8e [project @ 2005-05-06 19:10:17 by paulb] Added a special definition for the multivalue separator. Added extension functions for finding the names of fields for nodes other than the context node. Added conversion functions to translate field names into XPath expressions. Added preparation of template fragments for in-page updates of parts of a document. diff -r 5ac6333ec2a3 -r 2b1179c7ab81 XSLForms/Constants.py --- a/XSLForms/Constants.py Fri May 06 19:07:59 2005 +0000 +++ b/XSLForms/Constants.py Fri May 06 19:10:17 2005 +0000 @@ -4,6 +4,7 @@ path_separator = "/" pair_separator = "#" +multi_separator = "##" selector_indicator = "=" # vim: tabstop=4 expandtab shiftwidth=4 diff -r 5ac6333ec2a3 -r 2b1179c7ab81 XSLForms/Output.py --- a/XSLForms/Output.py Fri May 06 19:07:59 2005 +0000 +++ b/XSLForms/Output.py Fri May 06 19:10:17 2005 +0000 @@ -6,6 +6,7 @@ import Constants import libxslt +import libxml2 """ import libxml2 @@ -49,8 +50,7 @@ if name is not None: l.insert(0, name) if multivalue: - l.insert(0, Constants.pair_separator) - l.insert(0, Constants.pair_separator) + l.insert(0, Constants.multi_separator) l.insert(0, node.name) node = node.parent l.insert(0, Constants.path_separator) @@ -107,9 +107,41 @@ def new_field(context, name): return path_to_context(context, 0) + "/" + name +def other_field_name(context, nodes): + node = libxml2.xmlNode(nodes[0]) + return path_to_node(node, 0, None, 0) + +def other_multi_field_name(context, multivalue_name, nodes): + node = libxml2.xmlNode(nodes[0]) + return path_to_node(node, 1, multivalue_name, 1) + libxslt.registerExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_position) libxslt.registerExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", field_name) libxslt.registerExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name) libxslt.registerExtModuleFunction("new-field", "http://www.boddie.org.uk/ns/xmltools/template", new_field) +libxslt.registerExtModuleFunction("other-field-name", "http://www.boddie.org.uk/ns/xmltools/template", other_field_name) +libxslt.registerExtModuleFunction("other-multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", other_multi_field_name) + +def get_field_name(field_or_multi_name): + return field_or_multi_name.split(Constants.multi_separator)[0] + +def get_element_path(field_or_multi_name): + + """ + Convert the given 'field_or_multi_name' back to an XPath reference. + For example: + /configuration#1/base-system##value -> /configuration[1]/base-system + """ + + field_name = get_field_name(field_or_multi_name) + parts = field_name.split(Constants.path_separator) + new_parts = [] + for part in parts: + path_parts = part.split(Constants.pair_separator) + if len(path_parts) == 2: + new_parts.append(path_parts[0] + "[" + path_parts[1] + "]") + else: + new_parts.append(path_parts[0]) + return "/".join(new_parts) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 5ac6333ec2a3 -r 2b1179c7ab81 XSLForms/Prepare.py --- a/XSLForms/Prepare.py Fri May 06 19:07:59 2005 +0000 +++ b/XSLForms/Prepare.py Fri May 06 19:10:17 2005 +0000 @@ -16,4 +16,22 @@ template = libxml2dom.parse(template_name) proc.send_output(open(output_name, "wb"), encoding, template) +def make_stylesheet_fragment(template_name, output_name, element_id, stylesheet_name="Extract.xsl", encoding="utf-8"): + global resource_dir + proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"element-id" : element_id}) + template = libxml2dom.parse(template_name) + proc.send_output(open(output_name, "wb"), encoding, template) + +def ensure_stylesheet(template_name, output_name): + if not os.path.exists(output_name) or \ + os.path.getmtime(output_name) < os.path.getmtime(template_name): + + make_stylesheet(template_name, output_name) + +def ensure_stylesheet_fragment(template_name, output_name, element_id): + if not os.path.exists(output_name) or \ + os.path.getmtime(output_name) < os.path.getmtime(template_name): + + make_stylesheet_fragment(template_name, output_name, element_id) + # vim: tabstop=4 expandtab shiftwidth=4