xuri / xgen

XSD (XML Schema Definition) parser and Go/C/Java/Rust/TypeScript code generator
BSD 3-Clause "New" or "Revised" License
318 stars 75 forks source link

Generated code does not generate field for simple value #7

Closed fwielstra closed 2 years ago

fwielstra commented 4 years ago

Description

I have a (simplified) XSD as follows (partial but functional example):

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="attribute">
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="xs:string">
                    <xs:attribute type="xs:string" name="origin" use="required"/>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>
</xs:schema>

The XML that goes with it would look like this:

<?xml version="1.0" encoding="utf-8"?>
<attribute origin="reqavp">Destination-Host</attribute>

(I confirmed my XSD is valid and would produce the desired XML structure by copying it into http://xsd2xml.com/)

But the Go struct generated by xgen looks as follows:

// Copyright 2020 The xgen Authors. All rights reserved.
//
// DO NOT EDIT: generated by xgen XSD generator
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package schema

import (
    "encoding/xml"
)

// Attribute ...
type Attribute struct {
    XMLName    xml.Name `xml:"attribute"`
    OriginAttr string   `xml:"origin,attr"`
}

There is no field where I can put the 'value' (Destination-Host) of the attribute tag.

Is my XSD invalid, can I edit it to have the expected 'value' field, or is this an issue in the generator? I'm expecting a struct like:

type Attribute struct {
    XMLName    xml.Name `xml:"attribute"`
    OriginAttr string   `xml:"origin,attr"`
    Value      string   `xml:",chardata"`
}

Is there also a way to mark a struct field as xml:",cdata" ? Or will this involve editing the generated code?

BatmanAoD commented 4 years ago

I have the same issue.

alexandre-normand commented 2 years ago

@xuri : I believe this one should be good to close as fixed by https://github.com/xuri/xgen/pull/36.

xuri commented 2 years ago

Thank you very much for you work! @alexandre-normand