1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """Utility functions related to U{XML Namespaces<http://www.w3.org/TR/2006/REC-xml-names-20060816/index.html>}."""
17
18 import pyxb
19 import pyxb.namespace
20 import logging
21
22 _log = logging.getLogger(__name__)
23
35
37 """Given a URI, provide the L{Namespace} instance corresponding to it.
38
39 This can only be used to lookup or create real namespaces. To create
40 absent namespaces, use L{CreateAbsentNamespace}.
41
42 @param uri: The URI that identifies the namespace
43 @type uri: A non-empty C{str} or C{unicode} string
44 @keyword create_if_missing: If C{True}, a namespace for the given URI is
45 created if one has not already been registered. Default is C{False}.
46 @type create_if_missing: C{bool}
47 @return: The Namespace corresponding to C{uri}, if available
48 @rtype: L{Namespace} or C{None}
49 @raise pyxb.LogicError: The uri is not a non-empty string
50 """
51 if not isinstance(uri, (str, unicode)):
52 raise pyxb.LogicError('Cannot lookup absent namespaces')
53 if 0 == len(uri):
54 raise pyxb.LogicError('Namespace URIs must not be empty strings')
55 rv = pyxb.namespace.Namespace._NamespaceForURI(uri)
56 if (rv is None) and create_if_missing:
57 rv = pyxb.namespace.Namespace(uri)
58 return rv
59
61 """Create an absent namespace.
62
63 Use this when you need a namespace for declarations in a schema with no
64 target namespace. Absent namespaces are not stored in the infrastructure;
65 it is your responsibility to hold on to the reference you get from this,
66 because you won't be able to look it up."""
67 return pyxb.namespace.Namespace.CreateAbsentNamespace()
68
72