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