Skip to content
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

Add a 'Choice' field on elements #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 7 additions & 6 deletions xsd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func Normalize(docs ...[]byte) ([]*xmltree.Element, error) {
if err := nameAnonymousTypes(root, &typeCounter); err != nil {
return nil, err
}
if err := setChoicesOptional(root); err != nil {
if err := setChoices(root); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -238,9 +238,9 @@ func copyEltNamesToAnonTypes(root *xmltree.Element) {
}
}

// Inside a <xs:choice>, set all children to optional
// If child is a <xs:sequence> set its children to optional
func setChoicesOptional(root *xmltree.Element) error {
// Inside a <xs:choice>, set a "choice" attribute to represent that it is a child of a choice element
// If child is a <xs:sequence> set its children to the same choice
func setChoices(root *xmltree.Element) error {

for _, el := range root.SearchFunc(isElem(schemaNS, "choice")) {
for i := 0; i < len(el.Children); i++ {
Expand All @@ -249,11 +249,11 @@ func setChoicesOptional(root *xmltree.Element) error {
if t.Name.Space == schemaNS && t.Name.Local == "sequence" {
for j := 0; j < len(t.Children); j++ {
t2 := t.Children[j]
t2.SetAttr("", "minOccurs", "0")
t2.SetAttr("", "choice", strconv.Itoa(i+1))
t.Children[j] = t2
}
} else {
t.SetAttr("", "minOccurs", "0")
t.SetAttr("", "choice", strconv.Itoa(i+1))
}

el.Children[i] = t
Expand Down Expand Up @@ -823,6 +823,7 @@ func parseElement(ns string, el *xmltree.Element) Element {
Abstract: parseBool(el.Attr("", "abstract")),
Nillable: parseBool(el.Attr("", "nillable")),
Plural: parsePlural(el),
Choice: parseInt(el.Attr("", "choice")),
Scope: el.Scope,
}
if el.Attr("", "type") == "" {
Expand Down
3 changes: 3 additions & 0 deletions xsd/xsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type Element struct {
Optional bool
// If true, this element will be declared as a pointer.
Nillable bool
// If > 0, this element is inside a choice element.
// Represents the ordering of elements within a choice (Elements within a sequence share the same choice order).
Choice int
// Default overrides the zero value of this element.
Default string
// Any additional attributes provided in the <xs:element> element.
Expand Down