A Go tool/library that generates example XML snippets from XSD schemas. Given an XSD schema and an element name, it produces a valid XML example that matches the schema's structure.
- Generates example XML for any element defined in an XSD schema
- Handles complex types with nested elements
- Supports common XSD types (string, int, boolean, date, etc.)
- Optional namespace support with custom prefixes
As a command line tool:
go install github.com/outofcoffee/go-xml-example-generator@latestAs a dependency in your Go project:
go get github.com/outofcoffee/go-xml-example-generatorgo-xml-example-generator [xsd_path] [element_name]For example:
go-xml-example-generator schemas/petstore.xsd getPetByIdResponseThis will output something like:
<getPetByIdResponse>
<id>42</id>
<name>colour</name>
</getPetByIdResponse>Basic usage:
import "github.com/outofcoffee/go-xml-example-generator/examplegen"
// Generate XML from an XSD file
xml, err := examplegen.Generate("path/to/schema.xsd", "elementName")
if err != nil {
log.Fatal(err)
}
fmt.Println(xml)With namespace support:
// Generate XML with a namespace and prefix
xml, err := examplegen.GenerateWithNs("path/to/schema.xsd", "elementName", "urn:foo:bar", "foo")
if err != nil {
log.Fatal(err)
}
fmt.Println(xml)
// Output:
// <foo:elementName xmlns:foo="urn:foo:bar">
// <foo:childElement>value</foo:childElement>
// </foo:elementName>git clone https://github.com/outofcoffee/go-xml-example-generator.git
cd go-xml-example-generator
go buildgo test ./...- github.com/xuri/xgen - XML schema parser
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.