def get_xmlschema(xmlschema, mets_doc):
    """Return a ``class::lxml.etree.XMLSchema`` instance given the path to the
    XMLSchema (.xsd) file in ``xmlschema`` and the
    ``class::lxml.etree._ElementTree`` instance ``mets_doc`` representing the
    METS file being parsed. The complication here is that the METS file to be
    validated via the .xsd file may reference additional schemata via
    ``xsi:schemaLocation`` attributes. We have to find all of these and import
    them from within the returned XMLSchema.

    For the solution that this is based on, see:
    http://code.activestate.com/recipes/578503-validate-xml-with-schemalocation/

    For other descriptions of the problem, see:
    - https://groups.google.com/forum/#!topic/archivematica/UBS1ay-g_tE
    - https://stackoverflow.com/questions/26712645/xml-type-definition-is-absent
    - https://stackoverflow.com/questions/2979824/in-document-schema-declarations-and-lxml
    """
    xsd_path = _get_file_path(xmlschema)
    xmlschema = etree.parse(xsd_path)
    schema_locations = set(
        mets_doc.xpath("//*/@xsi:schemaLocation", namespaces=NAMESPACES)
    )
    for schema_location in schema_locations:
        namespaces_locations = schema_location.strip().split()
        for namespace, location in zip(*[iter(namespaces_locations)] * 2):
            if namespace == NAMESPACES["mets"]:
                continue
            xs_import = etree.Element("{http://www.w3.org/2001/XMLSchema}import")
            xs_import.attrib["namespace"] = namespace
            xs_import.attrib["schemaLocation"] = location
            xmlschema.getroot().insert(0, xs_import)
    return etree.XMLSchema(xmlschema)