Beispiel #1
0
def _get_target_namespace(xml_schema):
    """Get the target namespace.
    Args:
        xml_schema:  XML representation of the schema.

    Returns:
        The target namespace.

    """
    try:
        xsd_tree = XSDTree.transform_to_xml(xml_schema)
    except Exception as e:
        raise exceptions.XMLError(str(e))

    root = xsd_tree.find(".")
    if "targetNamespace" in root.attrib:
        target_namespace = root.attrib["targetNamespace"]
        if target_namespace not in list(root.nsmap.values()):
            message = "The use of a targetNamespace without an associated prefix is not supported."
            raise oai_pmh_exceptions.OAIAPILabelledException(
                message=message, status_code=status.HTTP_400_BAD_REQUEST)
    else:
        target_namespace = "http://www.w3.org/2001/XMLSchema"

    return target_namespace
Beispiel #2
0
    def fromstring(xml_string):
        """ Convert a string to an XML tree

        Args:
            xml_string:

        Returns:

        """
        try:
            return etree.fromstring(xml_string)
        except Exception as e:
            raise exceptions.XMLError(e.message)
Beispiel #3
0
    def tostring(xml_tree, pretty=False):
        """ Return an XML String from a lxml etree

        Args:
            xml_tree:
            pretty:

        Returns:

        """
        try:
            return etree.tostring(xml_tree, pretty_print=pretty)
        except Exception as e:
            raise exceptions.XMLError(e.message)
Beispiel #4
0
    def get_extension(xml_tree):
        """ Returns the extension file from a parsed xml

        Args:
            xml_tree: result of build_tree

        Returns:

        """
        try:
            return xml_tree.find("//xsl:output",
                                 namespaces={'xsl': xml_constants.XSL_NAMESPACE}).attrib['method']
        except Exception as e:
            raise exceptions.XMLError(e.message)
Beispiel #5
0
    def fromstring(xml_string, parser=None):
        """ Convert a string to an XML tree

        Args:
            xml_string:
            parser:

        Returns:

        """
        try:
            return etree.fromstring(xml_string, parser=parser)
        except Exception as e:
            raise exceptions.XMLError(str(e))
Beispiel #6
0
    def tostring(xml_tree, pretty=False):
        """Return an XML String from a lxml etree

        Args:
            xml_tree:
            pretty:

        Returns:

        """
        try:
            return etree.tostring(xml_tree, pretty_print=pretty, encoding="unicode")
        except Exception as e:
            raise exceptions.XMLError(str(e))
Beispiel #7
0
    def iterfind(xml_string, match):
        """ Finds all matching sub elements, by tag name or path.
        Args:
            xml_string: String xml.
            match: Pattern to match

        Returns:
            An iterable yielding all matching elements in document order.

        """
        try:
            xml_tree = XSDTree.build_tree(xml_string)
            return xml_tree.iterfind(match)
        except Exception as e:
            raise exceptions.XMLError(e.message)
Beispiel #8
0
    def iterparse(xml_string, events):
        """ Returns etree.iterparse

        Args:
            xml_string:
            events:

        Returns:

        """
        try:
            xml_file = BytesIO(str(xml_string))
            return etree.iterparse(xml_file, events)
        except Exception as e:
            raise exceptions.XMLError(e.message)
Beispiel #9
0
    def iterparse(xml_string, events):
        """ Returns etree.iterparse

        Args:
            xml_string:
            events:

        Returns:

        """
        try:
            xml_file = BytesIO(xml_string.encode('utf-8'))
            return etree.iterparse(xml_file, events)
        except Exception as e:
            raise exceptions.XMLError(str(e))
Beispiel #10
0
    def fromstring(xml_string, parser=None):
        """Convert a string to an XML tree

        Args:
            xml_string:
            parser:

        Returns:

        """
        if not parser:
            parser = XMLParser(remove_blank_text=True)
        try:
            return etree.fromstring(xml_string, parser=parser)
        except Exception as e:
            raise exceptions.XMLError(str(e))