Generates XML namespace declarations for namespaces in this
        set.  It must be suitable for use in an actual XML document,
        so an exception is raised if this can't be done, e.g. if it would
        have more than one default namespace declaration.

        If ``preferred_prefixes_only`` is ``True`` and a namespace's prefix
        preference is to be a default namespace, a default declaration will
        be used if possible.  If that's not possible, a prefix will be
        chosen (is this a good idea?).  If a default declaration can't be used
        and no other prefixes are defined, an exception is raised.

        Args:
            ns_uris (iterable): If non-None, it should be an iterable over
                namespace URIs.  Only the given namespaces will occur in the
                returned string.  If None, all namespace are included.
            sort (bool): If True, the string is constructed from URIs in sorted
                order.
            preferred_prefixes_only (bool): Whether to include only the
                preferred prefix or all of them, for each namespace.
            delim (str): The delimiter to use between namespace declarations.
                Should be some kind of whitespace.

        Returns:
            str: A string in the following format:
                ``xmlns:foo="bar"<delim>xmlns:foo2="bar2"<delim>...``

        Raises:
            NamespaceNotFoundError: If ``ns_uris`` is given and contains any
                URIs not in this set.
            TooManyDefaultNamespacesError: If too many namespaces didn't have
                a prefix.  The algorithm is very simple for deciding whose
                default preference is honored: the first default preference
                encountered gets to be default.  Any subsequent namespaces
                without any prefixes will cause this error.

def get_xmlns_string(self, ns_uris=None, sort=False,
                         preferred_prefixes_only=True, delim="\n"):
        """Generates XML namespace declarations for namespaces in this
        set.  It must be suitable for use in an actual XML document,
        so an exception is raised if this can't be done, e.g. if it would
        have more than one default namespace declaration.

        If ``preferred_prefixes_only`` is ``True`` and a namespace's prefix
        preference is to be a default namespace, a default declaration will
        be used if possible.  If that's not possible, a prefix will be
        chosen (is this a good idea?).  If a default declaration can't be used
        and no other prefixes are defined, an exception is raised.

        Args:
            ns_uris (iterable): If non-None, it should be an iterable over
                namespace URIs.  Only the given namespaces will occur in the
                returned string.  If None, all namespace are included.
            sort (bool): If True, the string is constructed from URIs in sorted
                order.
            preferred_prefixes_only (bool): Whether to include only the
                preferred prefix or all of them, for each namespace.
            delim (str): The delimiter to use between namespace declarations.
                Should be some kind of whitespace.

        Returns:
            str: A string in the following format:
                ``xmlns:foo="bar"<delim>xmlns:foo2="bar2"<delim>...``

        Raises:
            NamespaceNotFoundError: If ``ns_uris`` is given and contains any
                URIs not in this set.
            TooManyDefaultNamespacesError: If too many namespaces didn't have
                a prefix.  The algorithm is very simple for deciding whose
                default preference is honored: the first default preference
                encountered gets to be default.  Any subsequent namespaces
                without any prefixes will cause this error.
        """
        if ns_uris is None:
            ns_uris = self.namespace_uris

        if sort:
            ns_uris = sorted(ns_uris)

        have_default  = False  # Flag for default xmlns entry.
        xmlns_entries = []     # Stores all the xmlns:prefix=uri entries.

        for ns_uri in ns_uris:
            ni = self.__lookup_uri(ns_uri)

            if preferred_prefixes_only:
                if ni.preferred_prefix is not None:
                    xmlns = 'xmlns:{0.preferred_prefix}="{0.uri}"'.format(ni)
                    xmlns_entries.append(xmlns)
            else:
                xmlns = 'xmlns:{0}="{1.uri}"'
                xmlns_entries.extend(xmlns.format(prefix, ni) for prefix in ni.prefixes)

            if ni.preferred_prefix is None:
                if have_default:
                    # Already have a default namespace; try to choose a prefix
                    # for this one from the set of registered prefixes.
                    if len(ni.prefixes) == 0:
                        raise TooManyDefaultNamespacesError(ni.uri)
                    elif preferred_prefixes_only:
                        prefix = next(iter(ni.prefixes))
                        xmlns  = 'xmlns:{0}="{1.uri}"'.format(prefix, ni)
                        xmlns_entries.append(xmlns)

                    # else, we already declared some prefixes for this
                    # namespace, so don't worry about our inability to use this
                    # as a default namespace.
                else:
                    xmlns = 'xmlns="{0.uri}"'.format(ni)
                    xmlns_entries.append(xmlns)
                    have_default = True

        xmlns_str = delim.join(xmlns_entries) + delim
        return xmlns_str