# HG changeset patch # User paulb # Date 1126305183 0 # Node ID 1ba085b21c83c07a0f55d1f0f69cf1efcd6e81fb # Parent 2a86409f1b77f4e9bfba59fe326046ca39f5fb1b [project @ 2005-09-09 22:33:03 by paulb] Renamed the get_element_path function to element_path and made it an extension function "element-path". This makes the definition of in-page updates slightly more convenient. diff -r 2a86409f1b77 -r 1ba085b21c83 XSLForms/Output.py --- a/XSLForms/Output.py Fri Sep 09 21:02:59 2005 +0000 +++ b/XSLForms/Output.py Fri Sep 09 22:33:03 2005 +0000 @@ -312,6 +312,34 @@ output = "".join(l) return output +def element_path(context, field_names): + + """ + Convert the given 'field_names' back to XPath references. + For example: + /configuration$1/details$1/base-system$$value -> /*[position() = 1]/*[position() = 1]/base-system + If more than one field name is given - ie. 'field_names' contains a + comma-separated list of names - then only the first name is used. + """ + + field_name = field_names.split(",")[0] + + # Get the main part of the name (where a multivalue reference was given). + + field_name = get_field_name(field_name) + + # Build the XPath expression. + + 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("*[position() = " + path_parts[1] + "]") + else: + new_parts.append(path_parts[0]) + return "/".join(new_parts) + # New functions. libxsltmod.xsltRegisterExtModuleFunction("list-attribute", "http://www.boddie.org.uk/ns/xmltools/template", list_attribute) @@ -342,27 +370,9 @@ # Utility functions. libxsltmod.xsltRegisterExtModuleFunction("url-encode", "http://www.boddie.org.uk/ns/xmltools/template", url_encode) +libxsltmod.xsltRegisterExtModuleFunction("element-path", "http://www.boddie.org.uk/ns/xmltools/template", element_path) 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/details$1/base-system$$value -> /*[position() = 1]/*[position() = 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("*[position() = " + path_parts[1] + "]") - else: - new_parts.append(path_parts[0]) - return "/".join(new_parts) - # vim: tabstop=4 expandtab shiftwidth=4