2

I have been trying to use different Go packages for validating xml against xsd and no luck in efficiently using them.

github.com/goxmpp/xml

github.com/lestrrat-go/libxml2

github.com/go-xmlpath/xmlpath

github.com/metaleap/go-xsd/types

I either get compilation failures or unable to install the required dependencies.

Can anyone please provide a package available in Go and also a working example to test any xml against specified schema definition file (xsd)?

3
  • You can use powershell to validate : stackoverflow.com/questions/822907/…
    – jdweng
    Commented Aug 3, 2023 at 8:37
  • Thanks @jdweng but I need a program in Go language to run atleast on laptop.
    – user923499
    Commented Aug 3, 2023 at 8:56
  • Powershell is install on windows. Should run on your laptop.
    – jdweng
    Commented Aug 3, 2023 at 9:50

1 Answer 1

4

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.

5
  • Thanks! I get compile error, NewXsdHandlerFromFile not declared by package xsdvalidate and also runtime error, # pkg-config --cflags -- libxml-2.0 pkg-config: exec: "pkg-config": executable file not found in %PATH%
    – user923499
    Commented Aug 3, 2023 at 10:45
  • now issue solved? Commented Aug 6, 2023 at 16:06
  • same error coming Commented Oct 12, 2023 at 10:14
  • please confirm your gopath Commented Oct 16, 2023 at 17:57
  • go-xsd has been unluckily archived few months after you've suggested its use. xmlpath isn't active anymore (last commit 9 years ago). xsdvalidate looks the best choice then, but it involves three contributors only. That's one of the problem of Go: basic APIs that you find bundled with many other languages aren't provided immediately, and must be developed by open source community to exist. there many versions of bits of things and you wonder how long each will stand. Commented May 15 at 8:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.