From ac020f3140cd93db330ab05c55b65eec174987bf Mon Sep 17 00:00:00 2001 From: Marcus Cobden Date: Fri, 1 Mar 2019 14:05:18 +0000 Subject: [PATCH 1/4] Pass namespace through to attribute metadata --- xsd/parse.go | 1 + xsdgen/xsdgen.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/xsd/parse.go b/xsd/parse.go index 4edde53..ab1b059 100644 --- a/xsd/parse.go +++ b/xsd/parse.go @@ -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 diff --git a/xsdgen/xsdgen.go b/xsdgen/xsdgen.go index 5e01d4a..506a563 100644 --- a/xsdgen/xsdgen.go +++ b/xsdgen/xsdgen.go @@ -728,7 +728,7 @@ 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) + tag := fmt.Sprintf(`xml:"%s %s,attr%s"`, attr.Name.Space, 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) From 07ae29c2b3b698ad7275d2c2708bfeb8de4dd222 Mon Sep 17 00:00:00 2001 From: Marcus Cobden Date: Fri, 1 Mar 2019 14:05:47 +0000 Subject: [PATCH 2/4] Update tests and generated files to include namespace --- gentests/books/books_test.go | 2 +- xsdgen/example_test.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gentests/books/books_test.go b/gentests/books/books_test.go index 92e5f9f..76f29ff 100644 --- a/gentests/books/books_test.go +++ b/gentests/books/books_test.go @@ -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 { diff --git a/xsdgen/example_test.go b/xsdgen/example_test.go index b355d0e..79ce1cb 100644 --- a/xsdgen/example_test.go +++ b/xsdgen/example_test.go @@ -153,7 +153,7 @@ func ExampleReplace() { // // type StringArray struct { // Items []string `xml:",any"` - // ArrayType string `xml:"arrayType,attr,omitempty"` + // ArrayType string `xml:"http://www.example.com/ arrayType,attr,omitempty"` // } } @@ -180,9 +180,9 @@ func ExampleHandleSOAPArrayType() { // // type BoolArray struct { // Items []bool `xml:",any"` - // Offset string `xml:"offset,attr,omitempty"` - // Id string `xml:"id,attr,omitempty"` - // Href string `xml:"href,attr,omitempty"` + // Offset string `xml:"http://schemas.xmlsoap.org/soap/encoding/ offset,attr,omitempty"` + // Id string `xml:"http://schemas.xmlsoap.org/soap/encoding/ id,attr,omitempty"` + // Href string `xml:"http://schemas.xmlsoap.org/soap/encoding/ href,attr,omitempty"` // } } From ce25feb4551949471b36de24d0e23e685f203b57 Mon Sep 17 00:00:00 2001 From: Marcus Cobden Date: Mon, 4 Mar 2019 10:58:46 +0000 Subject: [PATCH 3/4] Only qualify attributes when needed --- gentests/books/books_test.go | 2 +- xsdgen/example_test.go | 8 ++++---- xsdgen/xsdgen.go | 13 ++++++++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/gentests/books/books_test.go b/gentests/books/books_test.go index 76f29ff..92e5f9f 100644 --- a/gentests/books/books_test.go +++ b/gentests/books/books_test.go @@ -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:"urn:books name,attr,omitempty"` + Name string `xml:"name,attr,omitempty"` } func (t *BookForm) MarshalXML(e *xml.Encoder, start xml.StartElement) error { diff --git a/xsdgen/example_test.go b/xsdgen/example_test.go index 79ce1cb..b355d0e 100644 --- a/xsdgen/example_test.go +++ b/xsdgen/example_test.go @@ -153,7 +153,7 @@ func ExampleReplace() { // // type StringArray struct { // Items []string `xml:",any"` - // ArrayType string `xml:"http://www.example.com/ arrayType,attr,omitempty"` + // ArrayType string `xml:"arrayType,attr,omitempty"` // } } @@ -180,9 +180,9 @@ func ExampleHandleSOAPArrayType() { // // type BoolArray struct { // Items []bool `xml:",any"` - // Offset string `xml:"http://schemas.xmlsoap.org/soap/encoding/ offset,attr,omitempty"` - // Id string `xml:"http://schemas.xmlsoap.org/soap/encoding/ id,attr,omitempty"` - // Href string `xml:"http://schemas.xmlsoap.org/soap/encoding/ href,attr,omitempty"` + // Offset string `xml:"offset,attr,omitempty"` + // Id string `xml:"id,attr,omitempty"` + // Href string `xml:"href,attr,omitempty"` // } } diff --git a/xsdgen/xsdgen.go b/xsdgen/xsdgen.go index 506a563..878b6f8 100644 --- a/xsdgen/xsdgen.go +++ b/xsdgen/xsdgen.go @@ -728,7 +728,18 @@ func (cfg *Config) genComplexType(t *xsd.ComplexType) ([]spec, error) { if attr.Optional { options = ",omitempty" } - tag := fmt.Sprintf(`xml:"%s %s,attr%s"`, attr.Name.Space, 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) From 137d4f75e73c55c1ef91031be9498afbc00dbc30 Mon Sep 17 00:00:00 2001 From: Marcus Cobden Date: Mon, 4 Mar 2019 11:15:33 +0000 Subject: [PATCH 4/4] Add test case to cover qualified attributes --- gentests/books/books.xml | 6 +++--- gentests/books/books.xsd | 2 +- gentests/books/books_test.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gentests/books/books.xml b/gentests/books/books.xml index 453af43..55caee4 100644 --- a/gentests/books/books.xml +++ b/gentests/books/books.xml @@ -1,6 +1,6 @@ - - + + Writer The First Book Fiction @@ -9,7 +9,7 @@ An amazing story of nothing. - + Poet The Poets First Poem Poem diff --git a/gentests/books/books.xsd b/gentests/books/books.xsd index 36bd496..6438769 100644 --- a/gentests/books/books.xsd +++ b/gentests/books/books.xsd @@ -22,7 +22,7 @@ - + diff --git a/gentests/books/books_test.go b/gentests/books/books_test.go index 92e5f9f..76f29ff 100644 --- a/gentests/books/books_test.go +++ b/gentests/books/books_test.go @@ -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 {