Ejemplo n.º 1
0
// Validate - Schemavalidate the document against the the schema file given in url
func (xp *Xp) SchemaValidate(url string) (errs []string, err error) {
	cSchemaNewMemParserCtxt := C.xmlSchemaNewParserCtxt((*C.char)(unsafe.Pointer(C.CString(url))))
	if cSchemaNewMemParserCtxt == nil {
		return nil, errors.New("Could not create schema parser")
	}
	defer C.xmlSchemaFreeParserCtxt(cSchemaNewMemParserCtxt)
	cSchema := C.xmlSchemaParse(cSchemaNewMemParserCtxt)
	if cSchema == nil {
		return nil, errors.New("Could not parse schema")
	}
	defer C.xmlSchemaFree(cSchema)

	validCtxt := C.xmlSchemaNewValidCtxt(cSchema)
	if validCtxt == nil {
		return nil, errors.New("Could not build validator")
	}
	defer C.xmlSchemaFreeValidCtxt(validCtxt)

	// void_libxml_error_handler is a null function - no info collected - just the final result matters - for now
	C.xmlSchemaSetValidErrors(validCtxt, (*[0]byte)(C.void_libxml_error_handler), (*[0]byte)(C.void_libxml_error_handler), nil)

	if errno := C.xmlSchemaValidateDoc(validCtxt, xp.doc); errno != 0 {
		return nil, fmt.Errorf("Document validation error %d", errno)
	}
	return nil, nil
}
Ejemplo n.º 2
0
// ParseSchema creates new Schema from []byte containing xml schema data.
// Will probably change []byte to DocPtr.
func ParseSchema(buffer []byte) (*Schema, error) {
	cSchemaNewMemParserCtxt := C.xmlSchemaNewMemParserCtxt((*C.char)(unsafe.Pointer(&buffer[0])), C.int(len(buffer)))
	if cSchemaNewMemParserCtxt == nil {
		return nil, errors.New("Could not create schema parser") // TODO extract error - see Validate func below
	}
	defer C.xmlSchemaFreeParserCtxt(cSchemaNewMemParserCtxt)
	cSchema := C.xmlSchemaParse(cSchemaNewMemParserCtxt)
	if cSchema == nil {
		return nil, errors.New("Could not parse schema") // TODO extract error - see Validate func below
	}
	return makeSchema(cSchema), nil
}