1.1 --- a/XSLForms/Output.py Tue Dec 21 00:46:33 2004 +0000
1.2 +++ b/XSLForms/Output.py Tue Dec 21 19:23:16 2004 +0000
1.3 @@ -4,6 +4,7 @@
1.4 XSL-based form templating.
1.5 """
1.6
1.7 +import Constants
1.8 import libxslt
1.9
1.10 """
1.11 @@ -15,12 +16,22 @@
1.12 libxml2.registerErrorHandler(quiet, None)
1.13 """
1.14
1.15 -def path_to_node(node, name):
1.16 +def path_to_node(node, attribute_ref, name):
1.17
1.18 """
1.19 - Generate an XSLForms path to the given 'node', using the given 'name' to
1.20 - complete the path if an attribute reference is required (otherwise 'name'
1.21 - will be None).
1.22 + Generate an XSLForms path to the given 'node', producing an attribute
1.23 + reference if 'attribute_ref' is true; for example:
1.24 +
1.25 + /package#1/discriminators#5/discriminator#1/category
1.26 +
1.27 + Otherwise an element reference is produced; for example:
1.28 +
1.29 + /package#1/discriminators#5/discriminator#1
1.30 +
1.31 + Use the given 'name' to complete the path if an attribute reference is
1.32 + required (and if a genuine attribute is found at the context node -
1.33 + otherwise 'name' will be None and the context node will be treated like an
1.34 + attribute).
1.35 """
1.36
1.37 l = []
1.38 @@ -28,24 +39,30 @@
1.39 if node.type == "attribute":
1.40 node = node.parent
1.41 # Manually insert the attribute name if defined.
1.42 - if name is not None:
1.43 - l.insert(0, name)
1.44 - l.insert(0, "/")
1.45 + if attribute_ref:
1.46 + if name is not None:
1.47 + l.insert(0, name)
1.48 + l.insert(0, Constants.path_separator)
1.49 + else:
1.50 + l.insert(0, node.name)
1.51 + l.insert(0, Constants.path_separator)
1.52 + node = node.parent
1.53 # Element references.
1.54 while node is not None and node.type != "document_xml":
1.55 l.insert(0, str(int(node.xpathEval("count(preceding-sibling::*) + 1"))))
1.56 - l.insert(0, "#")
1.57 + l.insert(0, Constants.pair_separator)
1.58 l.insert(0, node.name)
1.59 - l.insert(0, "/")
1.60 + l.insert(0, Constants.path_separator)
1.61 node = node.parent
1.62 return "".join(l)
1.63
1.64 -def this_position(context):
1.65 +def path_to_context(context, attribute_ref):
1.66
1.67 """
1.68 As a libxslt extension function, return a string containing the XSLForms
1.69 path to the 'context' node, using the special "this-name" variable to
1.70 - complete the path if an attribute reference is required.
1.71 + complete the path if an attribute reference is required (as indicated by
1.72 + 'attribute_ref' being set to true).
1.73 """
1.74
1.75 pctxt = libxslt.xpathParserContext(_obj=context)
1.76 @@ -57,8 +74,15 @@
1.77 name = name_var[0].content
1.78 else:
1.79 name = None
1.80 - return path_to_node(node, name)
1.81 + return path_to_node(node, attribute_ref, name)
1.82 +
1.83 +def this_position(context):
1.84 + return path_to_context(context, 0)
1.85 +
1.86 +def field_name(context):
1.87 + return path_to_context(context, 1)
1.88
1.89 libxslt.registerExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_position)
1.90 +libxslt.registerExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", field_name)
1.91
1.92 # vim: tabstop=4 expandtab shiftwidth=4