gws.lib.xmlx.namespace¶
Namespace table and XML name utilities.
Source code: gws.lib.xmlx.namespace
Module Contents¶
- gws.lib.xmlx.namespace.ADHOC = 'adhoc:'¶
{adhoc:foo}baris written asfoo:barand never declared.- Type:
URI scheme for undeclared prefixes
- gws.lib.xmlx.namespace.declarations(namespaces: list[gws.XmlNamespace], prefixes: dict[str, str] | None = None, with_schema_locations: bool = False) dict¶
Create
xmlnsdeclarations as attributes.- Parameters:
namespaces – Namespaces to declare (
prefix == ''is the default namespace).prefixes – Mapping from URIs to prefixes that replace the default ones.
with_schema_locations – Add an
xsi:schemaLocationattribute (and thexsideclaration) for namespaces that have a schema location.
- Returns:
A dict of attribute names and values, sorted by name.
- gws.lib.xmlx.namespace.find_by_prefix(prefix: str) gws.XmlNamespace | None¶
Find a well-known or registered namespace by its prefix.
Registered namespaces are checked first, then the well-known ones in the order of
namespace_c.- Parameters:
prefix – Namespace prefix.
- Returns:
The namespace, or
Noneif the prefix is unknown.
- gws.lib.xmlx.namespace.find_by_uri(uri: str) gws.XmlNamespace | None¶
Find a well-known or registered namespace by its URI.
If several well-known namespaces share the URI, the first one in
namespace_cis returned.- Parameters:
uri – Namespace URI.
- Returns:
The namespace, or
Noneif the URI is unknown.
- gws.lib.xmlx.namespace.find_well_known(name: str) gws.XmlNamespace | None¶
Find a well-known namespace by its constant name.
- Parameters:
name – Constant name in
namespace_c, e.g.OWS_11orGML.- Returns:
The namespace, or
Noneif there is no such name.
- gws.lib.xmlx.namespace.full_name(name: str, ns: gws.XmlNamespace | str | None) str¶
Create a Clark name (
{uri}name) from a name and a namespace or a URI.An existing prefix or URI in
nameis replaced.- Parameters:
name – A local, prefixed or Clark name.
ns – A namespace object or a URI.
- Returns:
The Clark name, or the local name if
nsis empty.
- gws.lib.xmlx.namespace.new(prefix: str, uri: str, schemaLocation: str = '', extendsGml: bool = False) gws.XmlNamespace¶
Create a namespace object.
The namespace is not registered.
- Parameters:
prefix – Default prefix, empty for a default namespace declaration.
uri – Namespace URI.
schemaLocation – Schema location URL.
extendsGml – Whether the namespace schema extends the GML schema.
- Returns:
A new namespace object.
- gws.lib.xmlx.namespace.parse_name(name: str) tuple[str, str, str]¶
Parse an XML name.
- Parameters:
name – A local (
foo), a prefixed (p:foo) or a Clark ({uri}foo) name.- Returns:
A tuple
(uri, prefix, local name);uriandprefixare empty when not present.
- gws.lib.xmlx.namespace.plain_name(name: str) str¶
Get the local part of an XML name.
- Parameters:
name – A local, prefixed or Clark name.
- Returns:
The local name.
- gws.lib.xmlx.namespace.register(ns: gws.XmlNamespace)¶
Register a custom namespace.
Registering the same prefix and URI again does nothing.
- Parameters:
ns – The namespace to register.
- Raises:
NamespaceError – If the URI is already known under a different prefix, or the prefix is already used for a different URI.
- gws.lib.xmlx.namespace.unregister_all()¶
Remove all registered custom namespaces.
Used in tests.
- gws.lib.xmlx.namespace.XML = 'xml'¶
Reserved
xmlprefix, never declared.
- gws.lib.xmlx.namespace.XML_URI = 'http://www.w3.org/XML/1998/namespace'¶
URI of the reserved
xmlprefix.
- gws.lib.xmlx.namespace.XMLNS = 'xmlns'¶
Prefix of namespace declarations.