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}bar is written as foo:bar and 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 xmlns declarations 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:schemaLocation attribute (and the xsi declaration) 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 None if 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_c is returned.

Parameters:

uri – Namespace URI.

Returns:

The namespace, or None if 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_11 or GML.

Returns:

The namespace, or None if 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 name is 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 ns is 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); uri and prefix are 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 xml prefix, never declared.

gws.lib.xmlx.namespace.XML_URI = 'http://www.w3.org/XML/1998/namespace'

URI of the reserved xml prefix.

gws.lib.xmlx.namespace.XMLNS = 'xmlns'

Prefix of namespace declarations.