paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | XSL-based form templating. |
paulb@116 | 5 | |
paulb@116 | 6 | Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@116 | 7 | |
paulb@116 | 8 | This library is free software; you can redistribute it and/or |
paulb@116 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@116 | 10 | License as published by the Free Software Foundation; either |
paulb@116 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@116 | 12 | |
paulb@116 | 13 | This library is distributed in the hope that it will be useful, |
paulb@116 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@116 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@116 | 16 | Lesser General Public License for more details. |
paulb@116 | 17 | |
paulb@116 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@116 | 19 | License along with this library; if not, write to the Free Software |
paulb@412 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@1 | 21 | """ |
paulb@1 | 22 | |
paulb@16 | 23 | import Constants |
paulb@539 | 24 | |
paulb@539 | 25 | # Try the conventional import first. |
paulb@539 | 26 | |
paulb@539 | 27 | try: |
paulb@539 | 28 | import libxsltmod, libxml2mod |
paulb@539 | 29 | except ImportError: |
paulb@539 | 30 | from libxmlmods import libxml2mod |
paulb@539 | 31 | from libxmlmods import libxsltmod |
paulb@539 | 32 | |
paulb@66 | 33 | import libxml2dom |
paulb@213 | 34 | import urllib |
paulb@1 | 35 | |
paulb@438 | 36 | libxml2_encoding = "utf-8" |
paulb@438 | 37 | |
paulb@23 | 38 | def path_to_node(node, attribute_ref, name, multivalue=0): |
paulb@1 | 39 | |
paulb@9 | 40 | """ |
paulb@16 | 41 | Generate an XSLForms path to the given 'node', producing an attribute |
paulb@16 | 42 | reference if 'attribute_ref' is true; for example: |
paulb@16 | 43 | |
paulb@139 | 44 | /package$1/discriminators$5/discriminator$1/category |
paulb@16 | 45 | |
paulb@16 | 46 | Otherwise an element reference is produced; for example: |
paulb@16 | 47 | |
paulb@139 | 48 | /package$1/discriminators$5/discriminator$1 |
paulb@16 | 49 | |
paulb@16 | 50 | Use the given 'name' to complete the path if an attribute reference is |
paulb@16 | 51 | required (and if a genuine attribute is found at the context node - |
paulb@16 | 52 | otherwise 'name' will be None and the context node will be treated like an |
paulb@16 | 53 | attribute). |
paulb@23 | 54 | |
paulb@156 | 55 | If 'multivalue' is true and 'attribute_ref' is set, produce an attribute |
paulb@156 | 56 | reference using the given 'name': |
paulb@23 | 57 | |
paulb@139 | 58 | /package$1/categories$1/category$$name |
paulb@156 | 59 | |
paulb@156 | 60 | If 'multivalue' is true and 'attribute_ref' is not set, produce an attribute |
paulb@156 | 61 | reference using the given 'name' of form (element, attribute): |
paulb@156 | 62 | |
paulb@156 | 63 | /package$1/categories$1/element$$attribute |
paulb@9 | 64 | """ |
paulb@1 | 65 | |
paulb@1 | 66 | l = [] |
paulb@6 | 67 | # Skip attribute reference. |
paulb@66 | 68 | if node.nodeType == node.ATTRIBUTE_NODE: |
paulb@66 | 69 | node = node.parentNode |
paulb@9 | 70 | # Manually insert the attribute name if defined. |
paulb@16 | 71 | if attribute_ref: |
paulb@23 | 72 | # A real attribute is referenced. |
paulb@16 | 73 | if name is not None: |
paulb@16 | 74 | l.insert(0, name) |
paulb@23 | 75 | if multivalue: |
paulb@49 | 76 | l.insert(0, Constants.multi_separator) |
paulb@66 | 77 | l.insert(0, node.nodeName) |
paulb@66 | 78 | node = node.parentNode |
paulb@16 | 79 | l.insert(0, Constants.path_separator) |
paulb@23 | 80 | # Otherwise, treat the element name as an attribute name. |
paulb@156 | 81 | # NOTE: Not sure how useful this is. |
paulb@16 | 82 | else: |
paulb@66 | 83 | l.insert(0, node.nodeName) |
paulb@16 | 84 | l.insert(0, Constants.path_separator) |
paulb@66 | 85 | node = node.parentNode |
paulb@156 | 86 | # Otherwise insert any multivalue references (eg. list-attribute). |
paulb@156 | 87 | elif multivalue: |
paulb@156 | 88 | element_name, attribute_name = name |
paulb@156 | 89 | l.insert(0, attribute_name) |
paulb@156 | 90 | l.insert(0, Constants.multi_separator) |
paulb@156 | 91 | l.insert(0, element_name) |
paulb@156 | 92 | l.insert(0, Constants.path_separator) |
paulb@156 | 93 | |
paulb@1 | 94 | # Element references. |
paulb@66 | 95 | while node is not None and node.nodeType != node.DOCUMENT_NODE: |
paulb@66 | 96 | l.insert(0, str(int(node.xpath("count(preceding-sibling::*) + 1")))) |
paulb@16 | 97 | l.insert(0, Constants.pair_separator) |
paulb@66 | 98 | l.insert(0, node.nodeName) |
paulb@16 | 99 | l.insert(0, Constants.path_separator) |
paulb@66 | 100 | node = node.parentNode |
paulb@1 | 101 | return "".join(l) |
paulb@1 | 102 | |
paulb@23 | 103 | def path_to_context(context, attribute_ref, multivalue_name=None): |
paulb@1 | 104 | |
paulb@1 | 105 | """ |
paulb@1 | 106 | As a libxslt extension function, return a string containing the XSLForms |
paulb@9 | 107 | path to the 'context' node, using the special "this-name" variable to |
paulb@16 | 108 | complete the path if an attribute reference is required (as indicated by |
paulb@23 | 109 | 'attribute_ref' being set to true). If 'multivalue_name' is set, produce a |
paulb@23 | 110 | reference to a multivalued field using the given string as the attribute |
paulb@23 | 111 | name. |
paulb@1 | 112 | """ |
paulb@1 | 113 | |
paulb@66 | 114 | context = libxml2mod.xmlXPathParserGetContext(context) |
paulb@66 | 115 | transform_context = libxsltmod.xsltXPathGetTransformContext(context) |
paulb@66 | 116 | name_var = libxsltmod.xsltVariableLookup(transform_context, "this-name", None) |
paulb@23 | 117 | if multivalue_name is not None: |
paulb@23 | 118 | name = multivalue_name |
paulb@23 | 119 | multivalue = 1 |
paulb@23 | 120 | elif name_var is not None: |
paulb@67 | 121 | name = libxml2mod.xmlNodeGetContent(name_var[0]) |
paulb@438 | 122 | name = unicode(name, libxml2_encoding) |
paulb@23 | 123 | multivalue = 0 |
paulb@9 | 124 | else: |
paulb@9 | 125 | name = None |
paulb@23 | 126 | multivalue = 0 |
paulb@66 | 127 | node = libxml2dom.Node(libxml2mod.xmlXPathGetContextNode(context)) |
paulb@23 | 128 | return path_to_node(node, attribute_ref, name, multivalue) |
paulb@16 | 129 | |
paulb@178 | 130 | # Exposed extension functions. |
paulb@178 | 131 | |
paulb@178 | 132 | def this_element(context): |
paulb@178 | 133 | |
paulb@178 | 134 | """ |
paulb@178 | 135 | Exposed as {template:this-element()}. |
paulb@382 | 136 | |
paulb@178 | 137 | Provides a reference to the current element in the form data structure. |
paulb@178 | 138 | """ |
paulb@178 | 139 | |
paulb@178 | 140 | #print "this_element" |
paulb@66 | 141 | r = path_to_context(context, 0) |
paulb@438 | 142 | return r.encode(libxml2_encoding) |
paulb@16 | 143 | |
paulb@178 | 144 | def this_attribute(context): |
paulb@178 | 145 | |
paulb@178 | 146 | """ |
paulb@178 | 147 | Exposed as {template:this-attribute()}. |
paulb@382 | 148 | |
paulb@178 | 149 | Provides a reference to the current attribute in the form data structure. |
paulb@178 | 150 | """ |
paulb@178 | 151 | |
paulb@178 | 152 | #print "this_attribute" |
paulb@66 | 153 | r = path_to_context(context, 1) |
paulb@438 | 154 | return r.encode(libxml2_encoding) |
paulb@1 | 155 | |
paulb@178 | 156 | def new_attribute(context, name): |
paulb@23 | 157 | |
paulb@178 | 158 | """ |
paulb@178 | 159 | Exposed as {template:new-attribute(name)}. |
paulb@382 | 160 | |
paulb@178 | 161 | Provides a reference to a new attribute of the given 'name' on the current |
paulb@178 | 162 | element in the form data structure. |
paulb@178 | 163 | """ |
paulb@178 | 164 | |
paulb@178 | 165 | #print "new_attribute" |
paulb@438 | 166 | name = unicode(name, libxml2_encoding) |
paulb@66 | 167 | r = path_to_context(context, 0) + "/" + name |
paulb@438 | 168 | return r.encode(libxml2_encoding) |
paulb@34 | 169 | |
paulb@178 | 170 | def other_elements(context, nodes): |
paulb@178 | 171 | |
paulb@178 | 172 | """ |
paulb@178 | 173 | Exposed as {template:other-elements(nodes)}. |
paulb@382 | 174 | |
paulb@178 | 175 | Provides a reference to other elements in the form data structure according |
paulb@178 | 176 | to the specified 'nodes' parameter (an XPath expression in the template). |
paulb@178 | 177 | """ |
paulb@178 | 178 | |
paulb@178 | 179 | #print "other_elements" |
paulb@57 | 180 | names = [] |
paulb@57 | 181 | for node in nodes: |
paulb@66 | 182 | name = path_to_node(libxml2dom.Node(node), 0, None, 0) |
paulb@57 | 183 | if name not in names: |
paulb@57 | 184 | names.append(name) |
paulb@66 | 185 | r = ",".join(names) |
paulb@438 | 186 | return r.encode(libxml2_encoding) |
paulb@49 | 187 | |
paulb@178 | 188 | def list_attribute(context, element_name, attribute_name): |
paulb@178 | 189 | |
paulb@178 | 190 | """ |
paulb@178 | 191 | Exposed as {template:list-attribute(element_name, attribute_name)}. |
paulb@382 | 192 | |
paulb@178 | 193 | Provides a reference to one or many elements of the given 'element_name' |
paulb@178 | 194 | found under the current element in the form data structure having |
paulb@178 | 195 | attributes with the given 'attribute_name'. |
paulb@178 | 196 | """ |
paulb@178 | 197 | |
paulb@178 | 198 | #print "list_attribute" |
paulb@438 | 199 | element_name = unicode(element_name, libxml2_encoding) |
paulb@438 | 200 | attribute_name = unicode(attribute_name, libxml2_encoding) |
paulb@178 | 201 | r = path_to_context(context, 0, (element_name, attribute_name)) |
paulb@438 | 202 | return r.encode(libxml2_encoding) |
paulb@178 | 203 | |
paulb@178 | 204 | def other_list_attributes(context, element_name, attribute_name, nodes): |
paulb@178 | 205 | |
paulb@178 | 206 | """ |
paulb@178 | 207 | Exposed as {template:other-list-attributes(element_name, attribute_name, nodes)}. |
paulb@382 | 208 | |
paulb@178 | 209 | Provides a reference to other elements in the form data structure, found |
paulb@178 | 210 | under the specified 'nodes' (described using an XPath expression in the |
paulb@178 | 211 | template) having the given 'element_name' and bearing attributes of the |
paulb@178 | 212 | given 'attribute_name'. |
paulb@178 | 213 | """ |
paulb@178 | 214 | |
paulb@178 | 215 | #print "other_list_attributes" |
paulb@438 | 216 | element_name = unicode(element_name, libxml2_encoding) |
paulb@438 | 217 | attribute_name = unicode(attribute_name, libxml2_encoding) |
paulb@178 | 218 | names = [] |
paulb@178 | 219 | for node in nodes: |
paulb@178 | 220 | name = path_to_node(libxml2dom.Node(node), 0, (element_name, attribute_name), 1) |
paulb@178 | 221 | if name not in names: |
paulb@178 | 222 | names.append(name) |
paulb@178 | 223 | r = ",".join(names) |
paulb@438 | 224 | return r.encode(libxml2_encoding) |
paulb@178 | 225 | |
paulb@178 | 226 | def other_attributes(context, attribute_name, nodes): |
paulb@178 | 227 | |
paulb@178 | 228 | """ |
paulb@178 | 229 | Exposed as {template:other-attributes(name, nodes)}. |
paulb@382 | 230 | |
paulb@178 | 231 | Provides a reference to attributes in the form data structure of the given |
paulb@178 | 232 | 'attribute_name' residing on the specified 'nodes' (described using an XPath |
paulb@178 | 233 | expression in the template). |
paulb@178 | 234 | """ |
paulb@178 | 235 | |
paulb@178 | 236 | #print "other_attributes" |
paulb@438 | 237 | attribute_name = unicode(attribute_name, libxml2_encoding) |
paulb@178 | 238 | # NOTE: Cannot directly reference attributes in the nodes list because |
paulb@178 | 239 | # NOTE: libxml2dom does not yet support parent element discovery on |
paulb@178 | 240 | # NOTE: attributes. |
paulb@178 | 241 | names = [] |
paulb@178 | 242 | for node in nodes: |
paulb@178 | 243 | name = path_to_node(libxml2dom.Node(node), 1, attribute_name, 0) |
paulb@178 | 244 | if name not in names: |
paulb@178 | 245 | names.append(name) |
paulb@178 | 246 | r = ",".join(names) |
paulb@438 | 247 | return r.encode(libxml2_encoding) |
paulb@178 | 248 | |
paulb@178 | 249 | def child_element(context, element_name, position, node_paths): |
paulb@178 | 250 | |
paulb@178 | 251 | """ |
paulb@178 | 252 | Exposed as {template:child-element(element_name, position, node_paths)}. |
paulb@382 | 253 | |
paulb@178 | 254 | Provides relative paths to the specifed 'element_name', having the given |
paulb@178 | 255 | 'position' (1-based) under each element specified in 'node_paths' (provided |
paulb@178 | 256 | by calls to other extension functions in the template). For example: |
paulb@178 | 257 | |
paulb@178 | 258 | template:child-element('comment', 1, template:this-element()) -> '.../comment$1' |
paulb@178 | 259 | """ |
paulb@178 | 260 | |
paulb@438 | 261 | element_name = unicode(element_name, libxml2_encoding) |
paulb@178 | 262 | l = [] |
paulb@178 | 263 | for node_path in node_paths.split(","): |
paulb@178 | 264 | l.append(node_path + Constants.path_separator + element_name |
paulb@178 | 265 | + Constants.pair_separator + str(int(position))) |
paulb@438 | 266 | return ",".join(l).encode(libxml2_encoding) |
paulb@178 | 267 | |
paulb@178 | 268 | def child_attribute(context, attribute_name, node_paths): |
paulb@178 | 269 | |
paulb@178 | 270 | """ |
paulb@178 | 271 | Exposed as {template:child-attribute(attribute_name, node_paths)}. |
paulb@382 | 272 | |
paulb@178 | 273 | Provides a relative path to the specifed 'attribute_name' for each element |
paulb@178 | 274 | specified in 'node_paths' (provided by calls to other extension functions in |
paulb@178 | 275 | the template). For example: |
paulb@178 | 276 | |
paulb@178 | 277 | template:child-attribute('value', template:this-element()) -> '.../value' |
paulb@178 | 278 | """ |
paulb@178 | 279 | |
paulb@438 | 280 | attribute_name = unicode(attribute_name, libxml2_encoding) |
paulb@178 | 281 | l = [] |
paulb@178 | 282 | for node_path in node_paths.split(","): |
paulb@178 | 283 | l.append(node_path + Constants.path_separator + attribute_name) |
paulb@438 | 284 | return ",".join(l).encode(libxml2_encoding) |
paulb@178 | 285 | |
paulb@301 | 286 | def selector_name(context, field_name, nodes): |
paulb@301 | 287 | |
paulb@301 | 288 | """ |
paulb@301 | 289 | Exposed as {template:selector-name(field_name, nodes)}. |
paulb@382 | 290 | |
paulb@301 | 291 | Provides a selector field name defined using 'field_name' and referring to |
paulb@301 | 292 | the given 'nodes'. For example: |
paulb@301 | 293 | |
paulb@301 | 294 | template:selector-name('add-platform', package/platforms) -> 'add-platform=/package$1/platforms$1' |
paulb@301 | 295 | |
paulb@301 | 296 | NOTE: The 'nodes' must be element references. |
paulb@301 | 297 | """ |
paulb@301 | 298 | |
paulb@301 | 299 | #print "selector_name" |
paulb@301 | 300 | names = [] |
paulb@301 | 301 | for node in nodes: |
paulb@301 | 302 | name = path_to_node(libxml2dom.Node(node), 0, None, 0) |
paulb@301 | 303 | if name not in names: |
paulb@301 | 304 | names.append(field_name + "=" + name) |
paulb@301 | 305 | r = ",".join(names) |
paulb@438 | 306 | return r.encode(libxml2_encoding) |
paulb@301 | 307 | |
paulb@178 | 308 | # Old implementations. |
paulb@178 | 309 | |
paulb@178 | 310 | def multi_field_name(context, multivalue_name): |
paulb@178 | 311 | #print "multi_field_name" |
paulb@438 | 312 | multivalue_name = unicode(multivalue_name, libxml2_encoding) |
paulb@178 | 313 | r = path_to_context(context, 1, multivalue_name) |
paulb@438 | 314 | return r.encode(libxml2_encoding) |
paulb@178 | 315 | |
paulb@57 | 316 | def other_multi_field_names(context, multivalue_name, nodes): |
paulb@67 | 317 | #print "other_multi_field_names" |
paulb@438 | 318 | multivalue_name = unicode(multivalue_name, libxml2_encoding) |
paulb@57 | 319 | names = [] |
paulb@57 | 320 | for node in nodes: |
paulb@66 | 321 | name = path_to_node(libxml2dom.Node(node), 1, multivalue_name, 1) |
paulb@57 | 322 | if name not in names: |
paulb@57 | 323 | names.append(name) |
paulb@66 | 324 | r = ",".join(names) |
paulb@438 | 325 | return r.encode(libxml2_encoding) |
paulb@49 | 326 | |
paulb@213 | 327 | # Utility functions. |
paulb@213 | 328 | |
paulb@454 | 329 | def xslforms_range(context, range_spec): |
paulb@454 | 330 | |
paulb@454 | 331 | """ |
paulb@454 | 332 | Exposed as {template:range(range_spec)}. |
paulb@454 | 333 | |
paulb@454 | 334 | The 'range_spec' is split up into 'start', 'finish' and 'step' according to |
paulb@454 | 335 | the following format: |
paulb@454 | 336 | |
paulb@454 | 337 | start...finish...step |
paulb@454 | 338 | |
paulb@454 | 339 | Provides the Python range function by producing a list of numbers, starting |
paulb@454 | 340 | at 'start', ending one step before 'finish', and employing the optional |
paulb@454 | 341 | 'step' to indicate the magnitude of the difference between successive |
paulb@454 | 342 | elements in the list as well as the "direction" of the sequence. By default, |
paulb@454 | 343 | 'step' is set to 1. |
paulb@454 | 344 | |
paulb@454 | 345 | NOTE: This uses a single string because template:element and other |
paulb@454 | 346 | NOTE: annotations use commas to separate fields, thus making the usage of |
paulb@454 | 347 | NOTE: this function impossible if each range parameter is exposed as a |
paulb@454 | 348 | NOTE: function parameter. |
paulb@454 | 349 | NOTE: The returning of values from this function is not fully verified, and |
paulb@454 | 350 | NOTE: it is probably better to use other extension functions instead of this |
paulb@454 | 351 | NOTE: one to achieve simple results (such as str:split from EXSLT). |
paulb@454 | 352 | """ |
paulb@454 | 353 | |
paulb@454 | 354 | parts = range_spec.split("...") |
paulb@454 | 355 | start, finish = parts[:2] |
paulb@454 | 356 | if len(parts) > 2: |
paulb@454 | 357 | step = parts[2] |
paulb@454 | 358 | else: |
paulb@454 | 359 | step = None |
paulb@454 | 360 | |
paulb@454 | 361 | start = int(start) |
paulb@454 | 362 | finish = int(finish) |
paulb@454 | 363 | if step is not None: |
paulb@454 | 364 | step = int(step) |
paulb@454 | 365 | else: |
paulb@454 | 366 | step = 1 |
paulb@454 | 367 | |
paulb@454 | 368 | # Create a list of elements. |
paulb@454 | 369 | # NOTE: libxslt complains: "Got a CObject" |
paulb@454 | 370 | |
paulb@454 | 371 | range_elements = libxml2mod.xmlXPathNewNodeSet(None) |
paulb@454 | 372 | for i in range(start, finish, step): |
paulb@454 | 373 | range_elements.append(libxml2mod.xmlNewText(str(i))) |
paulb@454 | 374 | return range_elements |
paulb@454 | 375 | |
paulb@434 | 376 | def i18n(context, value): |
paulb@434 | 377 | |
paulb@434 | 378 | """ |
paulb@434 | 379 | Exposed as {template:i18n(value)}. |
paulb@434 | 380 | |
paulb@434 | 381 | Provides a translation of the given 'value' using the 'translations' and |
paulb@443 | 382 | 'locale' variables defined in the output stylesheet. The 'value' may be a |
paulb@443 | 383 | string or a collection of nodes, each having a textual value, where such |
paulb@443 | 384 | values are then concatenated to produce a single string value. |
paulb@434 | 385 | """ |
paulb@434 | 386 | |
paulb@443 | 387 | if isinstance(value, str): |
paulb@443 | 388 | value = unicode(value, libxml2_encoding) |
paulb@443 | 389 | else: |
paulb@443 | 390 | l = [] |
paulb@443 | 391 | for node in value: |
paulb@443 | 392 | s = libxml2dom.Node(node).nodeValue |
paulb@443 | 393 | l.append(s) |
paulb@443 | 394 | value = "".join(l) |
paulb@443 | 395 | |
paulb@434 | 396 | context = libxml2mod.xmlXPathParserGetContext(context) |
paulb@434 | 397 | transform_context = libxsltmod.xsltXPathGetTransformContext(context) |
paulb@434 | 398 | translations_var = libxsltmod.xsltVariableLookup(transform_context, "translations", None) |
paulb@434 | 399 | locale_var = libxsltmod.xsltVariableLookup(transform_context, "locale", None) |
paulb@545 | 400 | if translations_var is not None and translations_var and locale_var is not None: |
paulb@434 | 401 | translations = libxml2dom.Node(translations_var[0]) |
paulb@434 | 402 | results = translations.xpath("/translations/locale[code/@value='%s']/translation[@value='%s']/text()" % (locale_var, value)) |
paulb@545 | 403 | if not results: |
paulb@545 | 404 | results = translations.xpath("/translations/locale[1]/translation[@value='%s']/text()" % value) |
paulb@545 | 405 | if results: |
paulb@438 | 406 | return results[0].nodeValue.encode(libxml2_encoding) |
paulb@438 | 407 | return value.encode(libxml2_encoding) |
paulb@434 | 408 | |
paulb@420 | 409 | def choice(context, value, true_string, false_string=None): |
paulb@420 | 410 | |
paulb@420 | 411 | """ |
paulb@420 | 412 | Exposed as {template:choice(value, true_string, false_string)}. |
paulb@420 | 413 | |
paulb@420 | 414 | Using the given boolean 'value', which may itself be an expression evaluated |
paulb@420 | 415 | by the XSLT processor, return the 'true_string' if 'value' is true or the |
paulb@420 | 416 | 'false_string' if 'value' is false. If 'false_string' is omitted and if |
paulb@420 | 417 | 'value' evaluates to a false value, an empty string is returned. |
paulb@420 | 418 | """ |
paulb@420 | 419 | |
paulb@420 | 420 | if value: |
paulb@420 | 421 | return true_string |
paulb@420 | 422 | else: |
paulb@420 | 423 | return false_string or "" |
paulb@420 | 424 | |
paulb@438 | 425 | def url_encode(context, nodes, charset=libxml2_encoding): |
paulb@213 | 426 | |
paulb@213 | 427 | """ |
paulb@213 | 428 | Exposed as {template:url-encode(nodes)}. |
paulb@382 | 429 | |
paulb@213 | 430 | Provides a "URL encoded" string created from the merged textual contents of |
paulb@213 | 431 | the given 'nodes', with the encoded character values representing characters |
paulb@417 | 432 | in the optional 'charset' (UTF-8 if not specified). Note that / and # |
paulb@417 | 433 | characters are replaced with their "URL encoded" character values. |
paulb@213 | 434 | |
paulb@418 | 435 | If a string value is supplied for 'nodes', this will be translated instead. |
paulb@418 | 436 | |
paulb@213 | 437 | template:url-encode(./text(), 'iso-8859-1') |
paulb@213 | 438 | """ |
paulb@213 | 439 | |
paulb@213 | 440 | l = [] |
paulb@418 | 441 | if isinstance(nodes, str): |
paulb@438 | 442 | return urllib.quote(nodes.encode(libxml2_encoding)).replace("/", "%2F").replace("#", "%23") |
paulb@418 | 443 | |
paulb@213 | 444 | for node in nodes: |
paulb@213 | 445 | s = libxml2dom.Node(node).nodeValue |
paulb@438 | 446 | l.append(urllib.quote(s.encode(libxml2_encoding)).replace("/", "%2F").replace("#", "%23")) |
paulb@213 | 447 | output = "".join(l) |
paulb@213 | 448 | return output |
paulb@213 | 449 | |
paulb@222 | 450 | def element_path(context, field_names): |
paulb@222 | 451 | |
paulb@222 | 452 | """ |
paulb@382 | 453 | Exposed as {template:element-path(field_names)}. |
paulb@382 | 454 | |
paulb@222 | 455 | Convert the given 'field_names' back to XPath references. |
paulb@222 | 456 | For example: |
paulb@382 | 457 | |
paulb@222 | 458 | /configuration$1/details$1/base-system$$value -> /*[position() = 1]/*[position() = 1]/base-system |
paulb@382 | 459 | |
paulb@222 | 460 | If more than one field name is given - ie. 'field_names' contains a |
paulb@222 | 461 | comma-separated list of names - then only the first name is used. |
paulb@382 | 462 | |
paulb@382 | 463 | To use this function effectively, use the result of another function as the |
paulb@382 | 464 | argument. For example: |
paulb@382 | 465 | |
paulb@382 | 466 | template:element-path(template:this-element()) |
paulb@382 | 467 | template:element-path(template:other-elements(matches)) |
paulb@382 | 468 | template:element-path(template:other-elements(..)) |
paulb@222 | 469 | """ |
paulb@222 | 470 | |
paulb@222 | 471 | field_name = field_names.split(",")[0] |
paulb@222 | 472 | |
paulb@222 | 473 | # Get the main part of the name (where a multivalue reference was given). |
paulb@222 | 474 | |
paulb@222 | 475 | field_name = get_field_name(field_name) |
paulb@222 | 476 | |
paulb@222 | 477 | # Build the XPath expression. |
paulb@222 | 478 | |
paulb@222 | 479 | parts = field_name.split(Constants.path_separator) |
paulb@222 | 480 | new_parts = [] |
paulb@222 | 481 | for part in parts: |
paulb@222 | 482 | path_parts = part.split(Constants.pair_separator) |
paulb@222 | 483 | if len(path_parts) == 2: |
paulb@222 | 484 | new_parts.append("*[position() = " + path_parts[1] + "]") |
paulb@222 | 485 | else: |
paulb@222 | 486 | new_parts.append(path_parts[0]) |
paulb@222 | 487 | return "/".join(new_parts) |
paulb@222 | 488 | |
paulb@156 | 489 | # New functions. |
paulb@156 | 490 | |
paulb@156 | 491 | libxsltmod.xsltRegisterExtModuleFunction("list-attribute", "http://www.boddie.org.uk/ns/xmltools/template", list_attribute) |
paulb@156 | 492 | libxsltmod.xsltRegisterExtModuleFunction("other-list-attributes", "http://www.boddie.org.uk/ns/xmltools/template", other_list_attributes) |
paulb@156 | 493 | libxsltmod.xsltRegisterExtModuleFunction("other-attributes", "http://www.boddie.org.uk/ns/xmltools/template", other_attributes) |
paulb@178 | 494 | libxsltmod.xsltRegisterExtModuleFunction("child-element", "http://www.boddie.org.uk/ns/xmltools/template", child_element) |
paulb@178 | 495 | libxsltmod.xsltRegisterExtModuleFunction("child-attribute", "http://www.boddie.org.uk/ns/xmltools/template", child_attribute) |
paulb@301 | 496 | libxsltmod.xsltRegisterExtModuleFunction("selector-name", "http://www.boddie.org.uk/ns/xmltools/template", selector_name) |
paulb@156 | 497 | |
paulb@144 | 498 | # New names. |
paulb@144 | 499 | |
paulb@178 | 500 | libxsltmod.xsltRegisterExtModuleFunction("this-element", "http://www.boddie.org.uk/ns/xmltools/template", this_element) |
paulb@178 | 501 | libxsltmod.xsltRegisterExtModuleFunction("this-attribute", "http://www.boddie.org.uk/ns/xmltools/template", this_attribute) |
paulb@178 | 502 | libxsltmod.xsltRegisterExtModuleFunction("new-attribute", "http://www.boddie.org.uk/ns/xmltools/template", new_attribute) |
paulb@178 | 503 | libxsltmod.xsltRegisterExtModuleFunction("other-elements", "http://www.boddie.org.uk/ns/xmltools/template", other_elements) |
paulb@144 | 504 | |
paulb@144 | 505 | # Old names. |
paulb@144 | 506 | |
paulb@178 | 507 | libxsltmod.xsltRegisterExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_element) |
paulb@178 | 508 | libxsltmod.xsltRegisterExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", this_attribute) |
paulb@178 | 509 | libxsltmod.xsltRegisterExtModuleFunction("new-field", "http://www.boddie.org.uk/ns/xmltools/template", new_attribute) |
paulb@178 | 510 | libxsltmod.xsltRegisterExtModuleFunction("other-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_elements) |
paulb@178 | 511 | |
paulb@178 | 512 | # Old functions. |
paulb@178 | 513 | |
paulb@66 | 514 | libxsltmod.xsltRegisterExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name) |
paulb@66 | 515 | libxsltmod.xsltRegisterExtModuleFunction("other-multi-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_multi_field_names) |
paulb@49 | 516 | |
paulb@213 | 517 | # Utility functions. |
paulb@213 | 518 | |
paulb@454 | 519 | libxsltmod.xsltRegisterExtModuleFunction("range", "http://www.boddie.org.uk/ns/xmltools/template", xslforms_range) |
paulb@434 | 520 | libxsltmod.xsltRegisterExtModuleFunction("i18n", "http://www.boddie.org.uk/ns/xmltools/template", i18n) |
paulb@420 | 521 | libxsltmod.xsltRegisterExtModuleFunction("choice", "http://www.boddie.org.uk/ns/xmltools/template", choice) |
paulb@213 | 522 | libxsltmod.xsltRegisterExtModuleFunction("url-encode", "http://www.boddie.org.uk/ns/xmltools/template", url_encode) |
paulb@222 | 523 | libxsltmod.xsltRegisterExtModuleFunction("element-path", "http://www.boddie.org.uk/ns/xmltools/template", element_path) |
paulb@213 | 524 | |
paulb@49 | 525 | def get_field_name(field_or_multi_name): |
paulb@49 | 526 | return field_or_multi_name.split(Constants.multi_separator)[0] |
paulb@49 | 527 | |
paulb@1 | 528 | # vim: tabstop=4 expandtab shiftwidth=4 |