diff --git a/xsd/parse.go b/xsd/parse.go index 0e6890d..6db1066 100644 --- a/xsd/parse.go +++ b/xsd/parse.go @@ -147,7 +147,14 @@ func Parse(docs ...[]byte) ([]Schema, error) { if err := s.parse(root); err != nil { return nil, err } - parsed[tns] = s + if existingSchema, found := parsed[tns]; found { + err = existingSchema.MergeTypes(&s) + if err != nil { + return nil, fmt.Errorf("error merging types into existing schema: %v", err) + } + } else { + parsed[tns] = s + } } for _, s := range parsed { diff --git a/xsd/xsd.go b/xsd/xsd.go index 1c1f6bb..35a58f3 100644 --- a/xsd/xsd.go +++ b/xsd/xsd.go @@ -19,6 +19,7 @@ import ( "fmt" "io" "regexp" + "strings" "time" "aqwari.net/xml/xmltree" @@ -110,6 +111,19 @@ type Schema struct { Doc string } +// MergeTypes adds the types from the incoming schema into +// the receiving schema, returns error if the schemas have +// different target namespaces. +func (s *Schema) MergeTypes(incoming *Schema) error { + if !strings.EqualFold(s.TargetNS, incoming.TargetNS) { + return fmt.Errorf("cannot merge types from %s into %s", incoming.TargetNS, s.TargetNS) + } + for n,t := range incoming.Types { + s.Types[n] = t + } + return nil +} + // FindType looks for a type by its canonical name. In addition to the types // declared in a Schema, FindType will also search through the types that // a Schema's top-level types are derived from. FindType will return nil if