从XSD创建一个Golang结构体

huangapple go评论90阅读模式
英文:

Create a Golang struct from an XSD

问题

我想从一个XSD(结构XSD)创建一个Golang结构体。

我已经阅读了这篇帖子https://stackoverflow.com/questions/20734450/generate-go-structs-from-xsd,它推荐使用go-xsd,但是我已经下载了go-xsd并安装了xsd-makepkg,但是我无法生成我的结构体。

我在做什么?

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

-xsd-makepkg:它是从go-xsd-pkg中创建的二进制文件
-basepath:包含我要转换为结构体的结构XSD的路径。
-goinst:我没有安装go-buildrun,我认为它不是必需的,所以设置为false。

命令的结果是什么?

一个包含其他文件夹的文件夹($GOPATH/usr/Users/XSD_Access/),这些文件夹包含所有的XML包装器。

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

结构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>

有人可以告诉我我做错了什么或者我错过了哪个步骤,导致我无法从我的结构XSD创建结构体吗?

提前感谢。

英文:

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

I've read the post https://stackoverflow.com/questions/20734450/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

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

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

答案1

得分: 5

xsd-makepkg期望从某种网络服务器下载xsd文件。

一开始我也对此感到困惑,因为文档中提到了从本地文件构建,但这只适用于已经下载的文件。

-basepath=&quot;&quot;确定这些文件将被下载到的位置,以及生成的.go文件将被放置的位置。

你要找的是-uri=&quot;&quot;参数。-uri=&quot;&quot;确定要下载和处理的哪些文件。该参数接受一个以空格分隔的URI列表,http://是可选的。

使其与本地文件一起工作的一种快速而简单的方法是从本地Apache实例中提供文件,然后将程序指向localhost。当然,这是假设你恰好有一个正在运行的Web服务器。

例如:

mv *.xsd /var/www/html
cd /var/www/html
for xsd in *; do xsd-makepkg -uri=&quot;127.0.0.1/$xsd&quot;; done
英文:

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=&quot;&quot; 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=&quot;&quot; argument. -uri=&quot;&quot; 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=&quot;127.0.0.1/$xsd&quot;; done

答案2

得分: 5

我找到了这个命令行选项:

-local=true:本地副本 -- 仅在本地文件不存在时下载

以下语句在 xsd-schema 文件夹中使用 mydomain.xsd 成功运行。

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

参考链接:https://github.com/metaleap/go-xsd#command-line-flags-for-go-xsdxsd-makepkg-tool

英文:

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

huangapple
  • 本文由 发表于 2015年1月21日 02:43:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/28052863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定