Skip to content

Pass namespace through to attribute metadata #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions gentests/books/books.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<books xmlns="urn:books">
<book name="bk001">
<books xmlns="urn:books" xmlns:books="urn:books">
<book books:name="bk001">
<author>Writer</author>
<title>The First Book</title>
<genre>Fiction</genre>
Expand All @@ -9,7 +9,7 @@
<review>An amazing story of nothing.</review>
</book>

<book name="bk002">
<book books:name="bk002">
<author>Poet</author>
<title>The Poets First Poem</title>
<genre>Poem</genre>
Expand Down
2 changes: 1 addition & 1 deletion gentests/books/books.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<xsd:element name="pub_date" type="xsd:date" />
<xsd:element name="review" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="name" form="qualified" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>

2 changes: 1 addition & 1 deletion gentests/books/books_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type BookForm struct {
Price float32 `xml:"urn:books price"`
Pubdate time.Time `xml:"urn:books pub_date"`
Review string `xml:"urn:books review"`
Name string `xml:"name,attr,omitempty"`
Name string `xml:"urn:books name,attr,omitempty"`
}

func (t *BookForm) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
Expand Down
1 change: 1 addition & 0 deletions xsd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ func parseAttribute(ns string, el *xmltree.Element) Attribute {
} else {
a.Name.Local = name
}
a.Name.Space = ns
a.Type = parseType(el.Resolve(el.Attr("", "type")))
a.Default = el.Attr("", "default")
a.Scope = el.Scope
Expand Down
13 changes: 12 additions & 1 deletion xsdgen/xsdgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,18 @@ func (cfg *Config) genComplexType(t *xsd.ComplexType) ([]spec, error) {
if attr.Optional {
options = ",omitempty"
}
tag := fmt.Sprintf(`xml:"%s,attr%s"`, attr.Name.Local, options)
qualified := false
for _, attrAttr := range attr.Attr {
if attrAttr.Name.Space == "" && attrAttr.Name.Local == "form" && attrAttr.Value == "qualified" {
qualified = true
}
}
var tag string
if qualified {
tag = fmt.Sprintf(`xml:"%s %s,attr%s"`, attr.Name.Space, attr.Name.Local, options)
} else {
tag = fmt.Sprintf(`xml:"%s,attr%s"`, attr.Name.Local, options)
}
base, err := cfg.expr(attr.Type)
if err != nil {
return nil, fmt.Errorf("%s attribute %s: %v", t.Name.Local, attr.Name.Local, err)
Expand Down