Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Golang struct from an XSD

Tags:

xml

go

xsd

I want to create a Golang struct from an XSD (Structure XSD).

I've read the post generate Go structs from XSD which recommend using go-xsd, but I have downloaded go-xsd and installed xsd-makepkg and I cannot generate my struct.

What I am doing?

xsd-makepkg -basepath="/Users/XSD_Access/" -goinst=false

-xsd-makepkg: it is the binary create from apart go-xsd-pkg
-basepath: Contains the route where I have Structure XSD that I want to transform to struct. -goinst : I have not installed go-buildrun and I think it is not neccesary , for that reason is ser false

What is the result of the command?

A folder($GOPATH/usr/Users/XSD_Access/) that contains other folders with all followers XML wrappers

  • docbook.org
  • docs.oasis-open.org
  • kbcafe.com
  • khronos.org
  • schemas.opengis.net
  • thearchitect.co.uk
  • Users
  • www.w3.org

Structure XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
   <xs:element name="Request" type="Request"/>
   <xs:complexType name="Request">
      <xs:annotation>
         <xs:documentation xml:lang="sp"><![CDATA[
        Comment xxxxx
     ]]></xs:documentation>
  </xs:annotation>
  <xs:sequence>
     <xs:element name="idOne" type="xs:string" minOccurs="0" maxOccurs="1">
      <xs:annotation>
     <xs:documentation xml:lang="sp"><![CDATA[Comment xxxxx
     ]]></xs:documentation>
      </xs:annotation>
    </xs:element>
         <xs:element name="idTwo" type="xs:string" minOccurs="0" maxOccurs="1">
  <xs:annotation>
     <xs:documentation xml:lang="sp"><![CDATA[Comment xxxxxx
     ]]></xs:documentation>
  </xs:annotation>
</xs:element>
  </xs:sequence>
   </xs:complexType>
</xs:schema>

Can anyone tell me what I am doing wrong or what step I missed that it does not let me create a struct from my Structure XSD?

Thanks in advance

like image 848
Carlos Andrés García Avatar asked Jan 20 '15 18:01

Carlos Andrés García


2 Answers

xsd-makepkg expects to download the xsd files from a network server of some sort.

I was confused by this at first as well, since the documentation mentioned building from local files, but that only works if the files specified are already downloaded.

-basepath="" determines where these files will be downloaded to, and where the generated .go files will be put

what you're looking for is the -uri="" argument. -uri="" determines which files are to be downloaded and processed. The argument takes a whitespace separated list of uris, and http:// is optional.

A quick and dirty way to make it work with a local file is to serve the file from a local Apache instance, then point the program to localhost. That is of course assuming that you happen to have a web server running.

for example:

mv *.xsd /var/www/html
cd /var/www/html
for xsd in *; do xsd-makepkg -uri="127.0.0.1/$xsd"; done
like image 163
kooshi_govno Avatar answered Oct 21 '22 14:10

kooshi_govno


I found this command line option:

-local=true :Local copy -- only downloads if file does not exist locally

The below statement worked with mydomain.xsd in the xsd-schema folder.

xsd-makepkg -local=true -uri="mydomain.xsd" -basepath="github.com/my_name/xsd-schema"

reference: https://github.com/metaleap/go-xsd#command-line-flags-for-go-xsdxsd-makepkg-tool

like image 23
Patrick Avatar answered Oct 21 '22 16:10

Patrick