1.1 --- a/XSLForms/Output.py Thu Dec 23 15:07:53 2004 +0000
1.2 +++ b/XSLForms/Output.py Sun Dec 26 18:47:08 2004 +0000
1.3 @@ -16,7 +16,7 @@
1.4 libxml2.registerErrorHandler(quiet, None)
1.5 """
1.6
1.7 -def path_to_node(node, attribute_ref, name):
1.8 +def path_to_node(node, attribute_ref, name, multivalue=0):
1.9
1.10 """
1.11 Generate an XSLForms path to the given 'node', producing an attribute
1.12 @@ -32,6 +32,11 @@
1.13 required (and if a genuine attribute is found at the context node -
1.14 otherwise 'name' will be None and the context node will be treated like an
1.15 attribute).
1.16 +
1.17 + If 'multivalue' is true, produce an attribute reference using the given
1.18 + 'name' of the following form:
1.19 +
1.20 + /package#1/categories#1/category##name
1.21 """
1.22
1.23 l = []
1.24 @@ -40,9 +45,16 @@
1.25 node = node.parent
1.26 # Manually insert the attribute name if defined.
1.27 if attribute_ref:
1.28 + # A real attribute is referenced.
1.29 if name is not None:
1.30 l.insert(0, name)
1.31 + if multivalue:
1.32 + l.insert(0, Constants.pair_separator)
1.33 + l.insert(0, Constants.pair_separator)
1.34 + l.insert(0, node.name)
1.35 + node = node.parent
1.36 l.insert(0, Constants.path_separator)
1.37 + # Otherwise, treat the element name as an attribute name.
1.38 else:
1.39 l.insert(0, node.name)
1.40 l.insert(0, Constants.path_separator)
1.41 @@ -56,13 +68,15 @@
1.42 node = node.parent
1.43 return "".join(l)
1.44
1.45 -def path_to_context(context, attribute_ref):
1.46 +def path_to_context(context, attribute_ref, multivalue_name=None):
1.47
1.48 """
1.49 As a libxslt extension function, return a string containing the XSLForms
1.50 path to the 'context' node, using the special "this-name" variable to
1.51 complete the path if an attribute reference is required (as indicated by
1.52 - 'attribute_ref' being set to true).
1.53 + 'attribute_ref' being set to true). If 'multivalue_name' is set, produce a
1.54 + reference to a multivalued field using the given string as the attribute
1.55 + name.
1.56 """
1.57
1.58 pctxt = libxslt.xpathParserContext(_obj=context)
1.59 @@ -70,11 +84,16 @@
1.60 node = context.contextNode()
1.61 transform_context = context.transformContext()
1.62 name_var = transform_context.variableLookup("this-name", None)
1.63 - if name_var is not None:
1.64 + if multivalue_name is not None:
1.65 + name = multivalue_name
1.66 + multivalue = 1
1.67 + elif name_var is not None:
1.68 name = name_var[0].content
1.69 + multivalue = 0
1.70 else:
1.71 name = None
1.72 - return path_to_node(node, attribute_ref, name)
1.73 + multivalue = 0
1.74 + return path_to_node(node, attribute_ref, name, multivalue)
1.75
1.76 def this_position(context):
1.77 return path_to_context(context, 0)
1.78 @@ -82,7 +101,11 @@
1.79 def field_name(context):
1.80 return path_to_context(context, 1)
1.81
1.82 +def multi_field_name(context, multivalue_name):
1.83 + return path_to_context(context, 1, multivalue_name)
1.84 +
1.85 libxslt.registerExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_position)
1.86 libxslt.registerExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", field_name)
1.87 +libxslt.registerExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name)
1.88
1.89 # vim: tabstop=4 expandtab shiftwidth=4