Here are a few packages that you can use to validate XML against XSD in Go:
xsdvalidate is a package that uses libxml2 to validate XML. It is a good choice if you need to validate XML quickly and efficiently.
go-xsd is another package that uses libxml2 to validate XML. It is a bit more complex to use than xsdvalidate
, but it offers more features.
xmlpath is a package that can be used to parse and validate XML. It is a good choice if you need to do more complex validations, such as checking for the presence of specific elements or attributes.
Here is a code example using xsdvalidate
:
package main
import (
"fmt"
"github.com/terminalstatic/go-xsd-validate"
)
func main() {
// Load the XSD file.
xsdHandler, err := xsdvalidate.NewXsdHandlerFromFile("my_schema.xsd")
if err != nil {
panic(err)
}
// Load the XML document.
xmlData, err := ioutil.ReadFile("my_xml.xml")
if err != nil {
panic(err)
}
// Validate the XML document against the XSD.
err = xsdHandler.Validate(xmlData)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("XML document is valid")
}
}
If you want an example for other packages please lemme know, then I can put something.