예제 #1
0
    def validate(self, schema):
        """
        Validate the XML in *file_* against a given schema.

|        *Parameters:*
|            *schema*........path to the schema file (*must* be RelaxNG)
|        *Return:*
|         True if schema validates successfully, False otherwise
        """

        try:
            from lxml import etree
            from lxml.etree import RelaxNG
        except ImportError:
            raise Exception(
                '[XMLObject.validate] Need lxml to validate xml!')

        try:
            relaxng_doc = etree.parse(schema)
            relaxng = RelaxNG(relaxng_doc)
            xml_doc = etree.parse(self._file)
        except etree.XMLSyntaxError as e:
            raise Exception(
                '[XMLObject.validate] Cannot validate xml: ' + str(e))

        return relaxng.validate(xml_doc)
예제 #2
0
    def validate(self, schema):
        """ 
        Validate the XML in *file_* against a given schema.

|        *Parameters:* 
|            *schema*........path to the schema file (*must* be RelaxNG)
|        *Return:* 
|         True if schema validates successfully, False otherwise 
        """

        try:
            from lxml import etree
            from lxml.etree import RelaxNG
        except ImportError:
            raise Exception('[XMLObject.validate] Need lxml to validate xml!')

        try:
            relaxng_doc = etree.parse(schema)
            relaxng = RelaxNG(relaxng_doc)
            xml_doc = etree.parse(self._file)
        except etree.XMLSyntaxError as e:
            raise Exception('[XMLObject.validate] Cannot validate xml: ' +
                            str(e))

        return relaxng.validate(xml_doc)
예제 #3
0
파일: __init__.py 프로젝트: scoder/mathdom
 def validate(self, xml_tree):
     if self._start is None:
         rng_tree = self._tree
     else:
         rng_tree = self._relocate_xslt(self._tree, newref="'%s'" % self._start)
         # ugly hack to get around namespace (?) issues
         rng_tree = parse(StringIO(str(rng_tree)))
     rng = RelaxNG(rng_tree)
     self.validate = rng.validate  # replace the object method by the real thing
     return rng.validate(xml_tree)
예제 #4
0
 def validate(self, xml_tree):
     if self._start is None:
         rng_tree = self._tree
     else:
         rng_tree = self._relocate_xslt(self._tree,
                                        newref="'%s'" % self._start)
         # ugly hack to get around namespace (?) issues
         rng_tree = parse(StringIO(str(rng_tree)))
     rng = RelaxNG(rng_tree)
     self.validate = rng.validate  # replace the object method by the real thing
     return rng.validate(xml_tree)
예제 #5
0
파일: xml.py 프로젝트: adbar/trafilatura
def validate_tei(xmldoc):  # , filename=""
    '''Check if an XML document is conform to the guidelines of the Text Encoding Initiative'''
    global TEI_RELAXNG
    if TEI_RELAXNG is None:
        # load validator
        with lzma.open(TEI_SCHEMA, 'rb') as schemafile:
            schema_data = load_pickle(schemafile)
        TEI_RELAXNG = RelaxNG(fromstring(schema_data))
    result = TEI_RELAXNG.validate(xmldoc)
    if result is False:
        LOGGER.warning('not a valid TEI document: %s', TEI_RELAXNG.error_log.last_error)
    return result