🔍 Code Extractor

function _process_namespace

Maturity: 32

Processes XML namespace prefixes in element/attribute names by resolving them against a namespace dictionary and reconstructing the full qualified name.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/xmltodict.py
Lines:
382 - 394
Complexity:
simple

Purpose

This function is used in XML parsing to resolve namespace prefixes to their full URIs. It takes a name (potentially with a namespace prefix), looks up the prefix in a namespaces dictionary, and returns the fully qualified name with the namespace URI. It handles both regular element names and attribute names (prefixed with '@'), and preserves the attribute prefix in the output if present in the input.

Source Code

def _process_namespace(name, namespaces, ns_sep=':', attr_prefix='@'):
    if not namespaces:
        return name
    try:
        ns, name = name.rsplit(ns_sep, 1)
    except ValueError:
        pass
    else:
        ns_res = namespaces.get(ns.strip(attr_prefix))
        name = '{}{}{}{}'.format(
            attr_prefix if ns.startswith(attr_prefix) else '',
            ns_res, ns_sep, name) if ns_res else name
    return name

Parameters

Name Type Default Kind
name - - positional_or_keyword
namespaces - - positional_or_keyword
ns_sep - ':' positional_or_keyword
attr_prefix - '@' positional_or_keyword

Parameter Details

name: The element or attribute name to process, potentially containing a namespace prefix separated by ns_sep (e.g., 'ns:elementName' or '@ns:attrName')

namespaces: A dictionary mapping namespace prefixes (strings) to their full namespace URIs. Can be None or empty dict if no namespace resolution is needed

ns_sep: The separator character used between namespace prefix and local name. Defaults to ':' which is standard for XML namespaces

attr_prefix: The prefix character used to denote attributes. Defaults to '@' which is commonly used in XML-to-dict conversions to distinguish attributes from elements

Return Value

Returns a string containing the processed name. If a namespace prefix was found and resolved, returns the name with the full namespace URI replacing the prefix (e.g., 'http://example.com/ns:elementName'). If no namespace is found or namespaces dict is empty, returns the original name unchanged. Preserves the attr_prefix if it was present in the input.

Usage Example

# Example 1: Basic namespace resolution
namespaces = {'soap': 'http://schemas.xmlsoap.org/soap/envelope/', 'xsi': 'http://www.w3.org/2001/XMLSchema-instance'}
name = 'soap:Envelope'
result = _process_namespace(name, namespaces)
# result: 'http://schemas.xmlsoap.org/soap/envelope/:Envelope'

# Example 2: Attribute with namespace
name = '@xsi:type'
result = _process_namespace(name, namespaces)
# result: '@http://www.w3.org/2001/XMLSchema-instance:type'

# Example 3: No namespace prefix
name = 'Body'
result = _process_namespace(name, namespaces)
# result: 'Body'

# Example 4: Empty namespaces
name = 'ns:element'
result = _process_namespace(name, {})
# result: 'ns:element'

# Example 5: Custom separator
name = 'ns.element'
result = _process_namespace(name, {'ns': 'http://example.com'}, ns_sep='.')
# result: 'http://example.com.element'

Best Practices

  • Ensure the namespaces dictionary contains all expected namespace prefixes before calling this function to avoid unresolved names
  • The function safely handles names without namespace prefixes by catching ValueError from rsplit
  • When using custom ns_sep or attr_prefix, ensure consistency across all XML processing operations
  • The function strips attr_prefix from namespace keys when looking up in the dictionary, so namespace dict keys should not include the attr_prefix
  • Returns the original name if namespace prefix is not found in the dictionary, allowing graceful degradation
  • This function is typically used internally by XML parsing libraries and should be called for each element/attribute name during XML-to-dict conversion

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class _DictSAXHandler 47.5% similar

    A SAX (Simple API for XML) event handler that converts XML documents into Python dictionaries, with extensive configuration options for handling attributes, namespaces, CDATA, and structure.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/xmltodict.py
  • function _emit 44.4% similar

    Recursively converts a dictionary structure into XML SAX events, emitting them through a content handler for XML generation.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/xmltodict.py
  • function xml_escape 44.3% similar

    Escapes special XML characters in a string by replacing them with their corresponding XML entity references.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/runtime/auth/providers/saml_token_provider.py
  • function parse 42.7% similar

    Parses XML input (string, file-like object, or generator) and converts it into a Python dictionary representation with configurable options for attributes, namespaces, comments, and streaming.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/xmltodict.py
  • function clean_text_for_xml_v1 40.7% similar

    Sanitizes text strings to ensure XML 1.0 compatibility by removing or replacing invalid control characters and ensuring all characters meet XML specification requirements for Word document generation.

    From: /tf/active/vicechatdev/enhanced_word_converter_fixed.py
← Back to Browse