1.1 --- a/XSLForms/Output.py Fri Jul 22 00:05:16 2005 +0000
1.2 +++ b/XSLForms/Output.py Fri Jul 22 18:26:38 2005 +0000
1.3 @@ -124,28 +124,51 @@
1.4 node = libxml2dom.Node(libxml2mod.xmlXPathGetContextNode(context))
1.5 return path_to_node(node, attribute_ref, name, multivalue)
1.6
1.7 -def this_position(context):
1.8 - #print "this_position"
1.9 +# Exposed extension functions.
1.10 +
1.11 +def this_element(context):
1.12 +
1.13 + """
1.14 + Exposed as {template:this-element()}.
1.15 + Provides a reference to the current element in the form data structure.
1.16 + """
1.17 +
1.18 + #print "this_element"
1.19 r = path_to_context(context, 0)
1.20 return r.encode("utf-8")
1.21
1.22 -def field_name(context):
1.23 - #print "field_name"
1.24 +def this_attribute(context):
1.25 +
1.26 + """
1.27 + Exposed as {template:this-attribute()}.
1.28 + Provides a reference to the current attribute in the form data structure.
1.29 + """
1.30 +
1.31 + #print "this_attribute"
1.32 r = path_to_context(context, 1)
1.33 return r.encode("utf-8")
1.34
1.35 -def multi_field_name(context, multivalue_name):
1.36 - #print "multi_field_name"
1.37 - r = path_to_context(context, 1, multivalue_name)
1.38 - return r.encode("utf-8")
1.39 +def new_attribute(context, name):
1.40
1.41 -def new_field(context, name):
1.42 - #print "new_field"
1.43 + """
1.44 + Exposed as {template:new-attribute(name)}.
1.45 + Provides a reference to a new attribute of the given 'name' on the current
1.46 + element in the form data structure.
1.47 + """
1.48 +
1.49 + #print "new_attribute"
1.50 r = path_to_context(context, 0) + "/" + name
1.51 return r.encode("utf-8")
1.52
1.53 -def other_field_names(context, nodes):
1.54 - #print "other_field_names"
1.55 +def other_elements(context, nodes):
1.56 +
1.57 + """
1.58 + Exposed as {template:other-elements(nodes)}.
1.59 + Provides a reference to other elements in the form data structure according
1.60 + to the specified 'nodes' parameter (an XPath expression in the template).
1.61 + """
1.62 +
1.63 + #print "other_elements"
1.64 names = []
1.65 for node in nodes:
1.66 name = path_to_node(libxml2dom.Node(node), 0, None, 0)
1.67 @@ -154,6 +177,99 @@
1.68 r = ",".join(names)
1.69 return r.encode("utf-8")
1.70
1.71 +def list_attribute(context, element_name, attribute_name):
1.72 +
1.73 + """
1.74 + Exposed as {template:list-attribute(element_name, attribute_name)}.
1.75 + Provides a reference to one or many elements of the given 'element_name'
1.76 + found under the current element in the form data structure having
1.77 + attributes with the given 'attribute_name'.
1.78 + """
1.79 +
1.80 + #print "list_attribute"
1.81 + r = path_to_context(context, 0, (element_name, attribute_name))
1.82 + return r.encode("utf-8")
1.83 +
1.84 +def other_list_attributes(context, element_name, attribute_name, nodes):
1.85 +
1.86 + """
1.87 + Exposed as {template:other-list-attributes(element_name, attribute_name, nodes)}.
1.88 + Provides a reference to other elements in the form data structure, found
1.89 + under the specified 'nodes' (described using an XPath expression in the
1.90 + template) having the given 'element_name' and bearing attributes of the
1.91 + given 'attribute_name'.
1.92 + """
1.93 +
1.94 + #print "other_list_attributes"
1.95 + names = []
1.96 + for node in nodes:
1.97 + name = path_to_node(libxml2dom.Node(node), 0, (element_name, attribute_name), 1)
1.98 + if name not in names:
1.99 + names.append(name)
1.100 + r = ",".join(names)
1.101 + return r.encode("utf-8")
1.102 +
1.103 +def other_attributes(context, attribute_name, nodes):
1.104 +
1.105 + """
1.106 + Exposed as {template:other-attributes(name, nodes)}.
1.107 + Provides a reference to attributes in the form data structure of the given
1.108 + 'attribute_name' residing on the specified 'nodes' (described using an XPath
1.109 + expression in the template).
1.110 + """
1.111 +
1.112 + #print "other_attributes"
1.113 + # NOTE: Cannot directly reference attributes in the nodes list because
1.114 + # NOTE: libxml2dom does not yet support parent element discovery on
1.115 + # NOTE: attributes.
1.116 + names = []
1.117 + for node in nodes:
1.118 + name = path_to_node(libxml2dom.Node(node), 1, attribute_name, 0)
1.119 + if name not in names:
1.120 + names.append(name)
1.121 + r = ",".join(names)
1.122 + return r.encode("utf-8")
1.123 +
1.124 +def child_element(context, element_name, position, node_paths):
1.125 +
1.126 + """
1.127 + Exposed as {template:child-element(element_name, position, node_paths)}.
1.128 + Provides relative paths to the specifed 'element_name', having the given
1.129 + 'position' (1-based) under each element specified in 'node_paths' (provided
1.130 + by calls to other extension functions in the template). For example:
1.131 +
1.132 + template:child-element('comment', 1, template:this-element()) -> '.../comment$1'
1.133 + """
1.134 +
1.135 + l = []
1.136 + for node_path in node_paths.split(","):
1.137 + l.append(node_path + Constants.path_separator + element_name
1.138 + + Constants.pair_separator + str(int(position)))
1.139 + return ",".join(l).encode("utf-8")
1.140 +
1.141 +def child_attribute(context, attribute_name, node_paths):
1.142 +
1.143 + """
1.144 + Exposed as {template:child-attribute(attribute_name, node_paths)}.
1.145 + Provides a relative path to the specifed 'attribute_name' for each element
1.146 + specified in 'node_paths' (provided by calls to other extension functions in
1.147 + the template). For example:
1.148 +
1.149 + template:child-attribute('value', template:this-element()) -> '.../value'
1.150 + """
1.151 +
1.152 + l = []
1.153 + for node_path in node_paths.split(","):
1.154 + l.append(node_path + Constants.path_separator + attribute_name)
1.155 + return ",".join(l).encode("utf-8")
1.156 +
1.157 +# Old implementations.
1.158 +
1.159 +def multi_field_name(context, multivalue_name):
1.160 + #print "multi_field_name"
1.161 + r = path_to_context(context, 1, multivalue_name)
1.162 + return r.encode("utf-8")
1.163 +
1.164 def other_multi_field_names(context, multivalue_name, nodes):
1.165 #print "other_multi_field_names"
1.166 names = []
1.167 @@ -164,56 +280,31 @@
1.168 r = ",".join(names)
1.169 return r.encode("utf-8")
1.170
1.171 -# New implementations.
1.172 -
1.173 -def list_attribute(context, element_name, attribute_name):
1.174 - #print "list_attribute"
1.175 - r = path_to_context(context, 0, (element_name, attribute_name))
1.176 - return r.encode("utf-8")
1.177 -
1.178 -def other_list_attributes(context, element_name, attribute_name, nodes):
1.179 - #print "other_list_attributes"
1.180 - names = []
1.181 - for node in nodes:
1.182 - name = path_to_node(libxml2dom.Node(node), 0, (element_name, attribute_name), 1)
1.183 - if name not in names:
1.184 - names.append(name)
1.185 - r = ",".join(names)
1.186 - return r.encode("utf-8")
1.187 -
1.188 -def other_attributes(context, attribute_name, nodes):
1.189 - #print "other_attributes"
1.190 - # NOTE: Cannot directly reference attributes in the nodes list because
1.191 - # NOTE: libxml2dom does not yet support parent element discovery on
1.192 - # NOTE: attributes.
1.193 - names = []
1.194 - for node in nodes:
1.195 - name = path_to_node(libxml2dom.Node(node), 1, attribute_name, 0)
1.196 - if name not in names:
1.197 - names.append(name)
1.198 - r = ",".join(names)
1.199 - return r.encode("utf-8")
1.200 -
1.201 # New functions.
1.202
1.203 libxsltmod.xsltRegisterExtModuleFunction("list-attribute", "http://www.boddie.org.uk/ns/xmltools/template", list_attribute)
1.204 libxsltmod.xsltRegisterExtModuleFunction("other-list-attributes", "http://www.boddie.org.uk/ns/xmltools/template", other_list_attributes)
1.205 libxsltmod.xsltRegisterExtModuleFunction("other-attributes", "http://www.boddie.org.uk/ns/xmltools/template", other_attributes)
1.206 +libxsltmod.xsltRegisterExtModuleFunction("child-element", "http://www.boddie.org.uk/ns/xmltools/template", child_element)
1.207 +libxsltmod.xsltRegisterExtModuleFunction("child-attribute", "http://www.boddie.org.uk/ns/xmltools/template", child_attribute)
1.208
1.209 # New names.
1.210
1.211 -libxsltmod.xsltRegisterExtModuleFunction("this-element", "http://www.boddie.org.uk/ns/xmltools/template", this_position)
1.212 -libxsltmod.xsltRegisterExtModuleFunction("this-attribute", "http://www.boddie.org.uk/ns/xmltools/template", field_name)
1.213 -libxsltmod.xsltRegisterExtModuleFunction("new-attribute", "http://www.boddie.org.uk/ns/xmltools/template", new_field)
1.214 -libxsltmod.xsltRegisterExtModuleFunction("other-elements", "http://www.boddie.org.uk/ns/xmltools/template", other_field_names)
1.215 +libxsltmod.xsltRegisterExtModuleFunction("this-element", "http://www.boddie.org.uk/ns/xmltools/template", this_element)
1.216 +libxsltmod.xsltRegisterExtModuleFunction("this-attribute", "http://www.boddie.org.uk/ns/xmltools/template", this_attribute)
1.217 +libxsltmod.xsltRegisterExtModuleFunction("new-attribute", "http://www.boddie.org.uk/ns/xmltools/template", new_attribute)
1.218 +libxsltmod.xsltRegisterExtModuleFunction("other-elements", "http://www.boddie.org.uk/ns/xmltools/template", other_elements)
1.219
1.220 # Old names.
1.221
1.222 -libxsltmod.xsltRegisterExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_position)
1.223 -libxsltmod.xsltRegisterExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", field_name)
1.224 +libxsltmod.xsltRegisterExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_element)
1.225 +libxsltmod.xsltRegisterExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", this_attribute)
1.226 +libxsltmod.xsltRegisterExtModuleFunction("new-field", "http://www.boddie.org.uk/ns/xmltools/template", new_attribute)
1.227 +libxsltmod.xsltRegisterExtModuleFunction("other-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_elements)
1.228 +
1.229 +# Old functions.
1.230 +
1.231 libxsltmod.xsltRegisterExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name)
1.232 -libxsltmod.xsltRegisterExtModuleFunction("new-field", "http://www.boddie.org.uk/ns/xmltools/template", new_field)
1.233 -libxsltmod.xsltRegisterExtModuleFunction("other-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_field_names)
1.234 libxsltmod.xsltRegisterExtModuleFunction("other-multi-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_multi_field_names)
1.235
1.236 def get_field_name(field_or_multi_name):