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
element
definitions and declarations, local definitions are hoisted to the package level attribute
-
simpleType
,complexType
andsimpleContent
,complexContent
-
sequence
andchoice
-
extension
,restriction
andenumeration
-
annotation
anddocumentation
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
):
-
indent
indent string, default 2 spaces -
break
line break char, default
or CR (carriage return) -
debug
write out schema types in comments, default 'no' -
buildtag
//go:build tags for the generated file, default empty -
targetNamespace
defaults to/xs:schema/@targetNamespace
-
package
Go package name, defaults tostr:tokenize(str:tokenize($targetNamespace, '/')[last()],'.')[1]
-
omitempty
whether to set omitempty modifier on field tags for optional and repeating elements, defaultyes
-
xmlns_xsd
defaults tohttp://www.w3.org/2001/XMLSchema
-
attributeForm
qualified or unqualified, defaults toxs:schema/@attributeFormDefault
-
qAttrType
data type to use for qualified attributes, defaults tostring
which is non-qualified, see PrefixAttr -
qAttrImport
import for the qualified attributes type package, e.gxmlext "github.com/indexdata/edge-slnp/utils"
. Note that this import is applied only whenattributeForm
is qualified -
dateTimeType
data type to use for XSD dateTime, defaults tostring
-
timeType
data type to use for XSD time, defaults tostring
-
dateType
data type to use for XSD date, defaults tostring
-
decimalType
data type to use for XSD decimal, defaults tofloat64
-
typeImports
comma-separated list of imports for additional type definitions used for date/time and decimal types -
json
should json tags be generated as well, defaultno
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.