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)
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)
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)
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)
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