go-xml icon indicating copy to clipboard operation
go-xml copied to clipboard

Unconvertable xsd schema

Open Toasterson opened this issue 5 years ago • 2 comments

Thank you very much for this hugely brilliant work.

I have a complex xsd file which triggers #86 and #96 but I can live with that as it is only one line I can insert myself. For documentation the offending line for those issues is here https://gist.github.com/Toasterson/5dd1db9c7862450c981ffd10f04d849b#file-service_bundle-xsd-L1339

However, it is also tripping somehow on the generated go code with:

service_bundle.go:3:7: expected 'IDENT', found 'struct' (and 1 more errors) in package smf

type  struct {
        Type Anon52 `xml:"type,attr"`
}
// May be one of
type Anon1 string
// May be one of constraints, values
type Anon52 string

type Any []string

func (a Any) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
        var output struct {
                ArrayType string   `xml:"http://schemas.xmlsoap.org/wsdl/ arrayType,attr"`
                Items     []string `xml:" item"`
        }
        output.Items = []string(a)
        start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{"", "xmlns:ns1"}, Value: "http://www.w3.org/2001/XMLSchema"})
        output.ArrayType = "ns1:anyType[]"
        return e.EncodeElement(&output, start)
}
func (a *Any) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
        var tok xml.Token
        for tok, err = d.Token(); err == nil; tok, err = d.Token() {
                if tok, ok := tok.(xml.StartElement); ok {
                        var item string
                        if err = d.DecodeElement(&item, &tok); err == nil {
                                *a = append(*a, item)
                        }
                }
                if _, ok := tok.(xml.EndElement); ok {
                        break
                }
        }
        return err
}

type Include struct {
        Fallback Any   `xml:" fallback"`
        Parse    Parse `xml:"parse,attr,omitempty"`
        Encoding       `xml:"encoding,attr,omitempty"`
}

func (t *Include) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
        type T Include
        var overlay struct {
                *T
                Parse *Parse `xml:"parse,attr,omitempty"`
        }
        overlay.T = (*T)(t)
        overlay.Parse = (*Parse)(&overlay.T.Parse)
        return d.DecodeElement(&overlay, &start)
}

type Lang string
// May be one of xml, text
type Parse string
// May be one of default, preserve
type Space string

All required xsd files are uploaded to a public gist. https://gist.github.com/Toasterson/5dd1db9c7862450c981ffd10f04d849b the original sources are open source.

I did convert this file from a dtd file with trang

If required I can also link the original dtd file.

Would love to use your tool to generate go types, as this allows me to write proper smf manifests on illumos systems.

Toasterson avatar Apr 15 '20 13:04 Toasterson

The error is here (note the extra space)

type  struct {
        Type Anon52 `xml:"type,attr"`
}

It seems similar to #99 . It appears to be trying to generate a complexType with an attribute named "type". There are a lot of those in the gist you provided. If I had to guess, I suspect it's this one, since there's no additional fields inside the complexType:

 <xs:element name="include_values">
   <xs:complexType>
     <xs:attribute name="type" use="required">
       <xs:simpleType>
         <xs:restriction base="xs:token">
           <xs:enumeration value="constraints"/>
           <xs:enumeration value="values"/>
         </xs:restriction>
       </xs:simpleType>
     </xs:attribute>
   </xs:complexType>
 </xs:element>

I'm not sure when I'll get a chance to look further, but could you try slimming down your xsd and see if you could pinpoint the type that causes this? Then maybe the -vvv output from xsdgen could give us a clue (warning: it prints a lot of output).

droyo avatar Apr 15 '20 16:04 droyo

I managed to get a working version with the alternate wool xgen and some heavy manual editing. Its at for a reference https://git.wegmueller.it/OpenCloud/opencloud/src/branch/master/smf/service_bundle.xsd.go

I tried to minify the file and see which type generates the error and it turns out pretty much every one does...

Example:

<xs:element name="doc_link">
    <xs:complexType>
      <xs:attribute name="name" use="required"/>
      <xs:attribute name="uri" use="required"/>
    </xs:complexType>
  </xs:element>
read service_bundle-min.xsd
read xml.xsd
setting namespaces to ["http://www.w3.org/XML/1998/namespace"]
complexType doc_link: could not find type  in namespace  for attribute name

I only found -vv as option not -vvv is that still enough output?

Toasterson avatar Apr 17 '20 08:04 Toasterson