xsd2go.xsl is an XSLT stylesheet that converts XML Schema to Go type definitions. Not all XSD elements are supported, but the most popular are:
- global and local
elementdefinitions and declarations, local definitions are hoisted to the package level attributesimpleType,complexTypeandsimpleContent,complexContentsequence,choiceandanyextension,restrictionandenumerationannotationanddocumentationimport
The stylesheet uses XSLT 1.0 plus a tokenize function from EXSLT as supported by xsltproc (libxml2)
xsltproc xsd2go.xsl some.xsd > some.go
The following parameters are supported (--stringparam in xsltproc):
buildtag//go:build tags for the generated file, default emptynamespacedifyesxml.Name field in global and hoisted structs and qualified attribute tags will use the target namespace, defaultnorootcomma-separated list of valid root elements, when set, only matching elements get namespace declaration or schema location attributesschemaLocationwhen set, generateMarshalXMLfor selected root element structs and emit schema location attributes. Can be just the schema path or a list of namespace/schema path pairs.namespaceImportscomma-separated list ofnamespace-uri=package-pathmappings to use when convertingxs:importelements to Go package importspackageGo package name, defaults tostr:tokenize(str:tokenize($targetNamespace, '/')[last()],'.')[1]omitemptywhether to set omitempty modifier on field tags for optional and repeating elements, defaultyesjsonshould json tags be generated as well, defaultnovalidateshould go-playground/validator tags be generated as well, defaultnotypeImportscomma-separated list of imports for additional type definitions used for date/time and decimal typesdateTimeTypedata type to use for XSD dateTime, defaults tostringtimeTypedata type to use for XSD time, defaults tostringdateTypedata type to use for XSD date, defaults tostringdecimalTypedata type to use for XSD decimal, defaults tofloat64qAttrTypedata type to use for qualified attributes, defaults tostringwhich is non-qualified, see PrefixAttrqAttrImportimport for the qualified attributes type package, e.gxmlext "github.com/indexdata/edge-slnp/utils". Note that this import is applied only whenattributeFormis qualifiedindentindent string, default 2 spacesbreakline break char, default or CR (carriage return)debugwrite out schema types in comments, default 'no'targetNamespacedefaults to/xs:schema/@targetNamespaceattributeFormqualified or unqualified, defaults toxs:schema/@attributeFormDefaultxmlns_xsddefaults tohttp://www.w3.org/2001/XMLSchema
You can also see rendered Go structs in the browser by prepending:
<?xml-stylesheet type="text/xsl" href="xsd2go.xsl"?>
to the XSD and opening the file in the browser, best by serving it with a local HTTP server like python3 -m http.server to avoid local security constraints. This works in Firefox but fails silently in Chrome, likely because of missing EXSLT support.
This repo includes a simple Go wrapper over xsltproc which you can run with:
go run github.com/indexdata/xsd2goxsl <in.xsd> <out.go> <param-name=param-value>,...
e.g
go run github.com/indexdata/xsd2goxsl xsd/ncip_v2_02.xsd ncip/schema.go "qAttrImport=utils \"github.com/indexdata/go-utils/utils\"" qAttrType=utils.PrefixAttr dateTimeType=utils.XSDDateTime
This allows using it in a Go project during the build with go generate. E.g by adding a Go xsd-gen.go file to the project with:
package ncip
//go:generate go run github.com/indexdata/xsd2goxsl xsd/ncip_v2_02.xsd ncip/schema.go "qAttrImport=utils \"github.com/indexdata/go-utils/utils\"" qAttrType=utils.PrefixAttr dateTimeType=utils.XSDDateTime decimalType=utils.XSDDecimal
and running:
go generate
Additionally, you can add a disabled source file with an import for this project to force Go handling it as a dependency:
//go:build tools
package tools
//build-time toolchain dependencies
import (
_ "github.com/indexdata/xsd2goxsl"
)
There are example XSDs and corresponding generated Go models under ./xsd.