Verifies whether the validity of the signature contained in an XML
    document following the XMLDSig standard

def verify(xml):
    '''Verifies whether the validity of the signature contained in an XML
    document following the XMLDSig standard'''
    try:
        from cStringIO import cStringIO as StringIO 
    except ImportError:
        from StringIO import StringIO
    
    xml = xml.encode('utf-8', 'xmlcharrefreplace')
    tree = etree.parse(StringIO(xml))
    
    try:
        '''Isolating the Signature element'''
        signature_tree = tree.xpath('xmldsig:Signature',
                                    namespaces=NAMESPACES)[0]
    
        '''Removing the Signature element from the main tree to compute a new 
        digest value with the actual content of the XML.'''
        signature_tree.getparent().remove(signature_tree)
        signed_info = _generate_signed_info(etree.tostring(tree))
        
        '''Constructing the key from the Modulus and Exponent stored in the
        Signature/KeyInfo/KeyValue/RSAKeyValue element'''                                 
        from Crypto.PublicKey import RSA
        modulus = signature_tree.xpath(KEY_INFO_PATH + '/xmldsig:Modulus',
                                       namespaces=NAMESPACES)[0].text
        exponent = signature_tree.xpath(KEY_INFO_PATH + '/xmldsig:Exponent',
                                        namespaces=NAMESPACES)[0].text        
        public_key = RSA.construct((b64d(modulus), b64d(exponent)))
    
        '''Isolating the signature value to verify'''
        signature = signature_tree.xpath('xmldsig:SignatureValue',
                                         namespaces=NAMESPACES)[0].text
    except IndexError:
        raise RuntimeError('XML does not contain a properly formatted ' \
                           ' Signature element')
    
    verifier = PKCS1_v1_5.PKCS115_SigScheme(public_key)
    return verifier.verify(SHA.new(c14n(signed_info)),
                           binascii.a2b_base64(signature))