Skip to content

Commit d9421b2

Browse files
authored
Merge pull request #135 from janettework/opt-choice-elements
Set elements inside <xs:choice> to optional
2 parents 55dcb4d + 3cf9133 commit d9421b2

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

xsd/parse.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ func Normalize(docs ...[]byte) ([]*xmltree.Element, error) {
114114
if err := nameAnonymousTypes(root, &typeCounter); err != nil {
115115
return nil, err
116116
}
117+
if err := setChoicesOptional(root); err != nil {
118+
return nil, err
119+
}
117120
}
118121
for _, root := range result {
119122
expandComplexShorthand(root)
@@ -235,6 +238,31 @@ func copyEltNamesToAnonTypes(root *xmltree.Element) {
235238
}
236239
}
237240

241+
// Inside a <xs:choice>, set all children to optional
242+
// If child is a <xs:sequence> set its children to optional
243+
func setChoicesOptional(root *xmltree.Element) error {
244+
245+
for _, el := range root.SearchFunc(isElem(schemaNS, "choice")) {
246+
for i := 0; i < len(el.Children); i++ {
247+
t := el.Children[i]
248+
249+
if t.Name.Space == schemaNS && t.Name.Local == "sequence" {
250+
for j := 0; j < len(t.Children); j++ {
251+
t2 := t.Children[j]
252+
t2.SetAttr("", "minOccurs", "0")
253+
t.Children[j] = t2
254+
}
255+
} else {
256+
t.SetAttr("", "minOccurs", "0")
257+
}
258+
259+
el.Children[i] = t
260+
}
261+
}
262+
263+
return nil
264+
}
265+
238266
/*
239267
Convert
240268

0 commit comments

Comments
 (0)