@@ -48,7 +48,7 @@ parseChild :: Cursor -> P (Maybe Xsd.Child)
4848parseChild c = do
4949 name <- getElementName c
5050 case name of
51− "element" -> Just . Xsd.ChildElement <$> parseElement c
51+ "element" -> Just . Xsd.ChildElement <$> parseElementOrRef c
5252 "simpleType" -> Just <$> parseTopSimpleType c
5353 "complexType" -> Just <$> parseTopComplexType c
5454 "include" -> Just . Xsd.ChildInclude <$> parseInclude c
@@ -81,14 +81,19 @@ parseImport c = do
8181 , Xsd.importNamespace = namespace
8282 }
8383
84+parseElementOrRef :: Cursor -> P (Xsd.RefOr Xsd.Element)
85+parseElementOrRef c = do
86+ case anAttribute "ref" c of
87+ Nothing -> Xsd.Inline <$> parseElement c
88+ Just ref -> Xsd.Ref <$> makeQName c ref
89+
8490parseElement :: Cursor -> P Xsd.Element
8591parseElement c = handleNamespaces c $ do
8692 name <- theAttribute "name" c
8793
88− -- XXX: generalaze RefOr parsing
8994 tp <- case anAttribute "type" c of
9095 Just t -> Xsd.Ref <$> makeQName c t
91− _ -> Xsd.Inline <$> parseType c
96+ _ -> maybe (Xsd.Ref anyType) Xsd.Inline <$> parseType c
9297
9398 minOccurs <- case anAttribute "minOccurs" c of
9499 Nothing -> return 1
@@ -120,16 +125,18 @@ parseElement c = handleNamespaces c $ do
120125 , Xsd.elementOccurs = (minOccurs, maxOccurs)
121126 , Xsd.elementNillable = nillable
122127 }
128+ where
129+ anyType = Xsd.QName (Just (Xsd.Namespace Xsd.schemaNamespace)) "anyType"
123130
124131-- | Parse inline type, simple or complex
125−parseType :: Cursor -> P Xsd.Type
132+parseType :: Cursor -> P (Maybe Xsd.Type)
126133parseType c = do
127134 simpleTypeAxis <- makeElemAxis "simpleType"
128135 complexTypeAxis <- makeElemAxis "complexType"
129136 case (c $/ simpleTypeAxis, c $/ complexTypeAxis) of
130− ([], []) -> parseError c "No type"
131− ([t], []) -> Xsd.TypeSimple <$> parseSimpleType t
132− ([], [t]) -> Xsd.TypeComplex <$> parseComplexType t
137+ ([], []) -> return Nothing
138+ ([t], []) -> Just . Xsd.TypeSimple <$> parseSimpleType t
139+ ([], [t]) -> Just . Xsd.TypeComplex <$> parseComplexType t
133140 _ -> parseError c "Multiple types"
134141
135142-- | Top level simple type
@@ -153,18 +160,19 @@ parseSimpleType c = do
153160 unionAxis <- makeElemAxis "union"
154161 case (c $/ restrictionAxis, c $/ listAxis, c $/ unionAxis) of
155162 ([e], [], []) -> Xsd.AtomicType
156− <$> parseRestriction e
163+ <$> parseSimpleRestriction e
157164 <*> parseAnnotations c
158165 ([], [l], []) -> Xsd.ListType
159166 <$> parseList l
160167 <*> parseAnnotations c
161− -- TODO implement me
162− ([], [], [_]) -> parseError c "Not implemented: union"
168+ ([], [], [u]) -> Xsd.UnionType
169+ <$> parseUnion u
170+ <*> parseAnnotations c
163171 _ -> parseError c "Expected exactly of of restriction, list or union"
164172
165173-- | Restriction for simple type
166−parseRestriction :: Cursor -> P Xsd.Restriction
167−parseRestriction c = do
174+parseSimpleRestriction :: Cursor -> P Xsd.SimpleRestriction
175+parseSimpleRestriction c = do
168176 -- XXX: generalaze RefOr parsing
169177 tp <- case anAttribute "base" c of
170178 Nothing -> do
@@ -177,9 +185,9 @@ parseRestriction c = do
177185
178186 constraints <- parseConstrains c
179187
180− return Xsd.Restriction
181− { Xsd.restrictionBase = tp
182− , Xsd.restrictionConstraints = constraints
188+ return Xsd.SimpleRestriction
189+ { Xsd.simpleRestrictionBase = tp
190+ , Xsd.simpleRestrictionConstraints = constraints
183191 }
184192
185193parseConstrains :: Cursor -> P [Xsd.Constraint]
@@ -200,6 +208,15 @@ parseList c =
200208 _ -> parseError c "Multiple types"
201209 Just t -> Xsd.Ref <$> makeQName c t
202210
211+parseUnion :: Cursor -> P [Xsd.RefOr Xsd.SimpleType]
212+parseUnion c =
213+ case anAttribute "memberTypes" c of
214+ Just list ->
215+ mapM (fmap Xsd.Ref . makeQName c) (Text.splitOn " " list)
216+ Nothing -> do
217+ simpleTypeAxis <- makeElemAxis "simpleType"
218+ mapM (fmap Xsd.Inline . parseSimpleType) (c $/ simpleTypeAxis)
219+
203220parseComplexType :: Cursor -> P Xsd.ComplexType
204221parseComplexType c = do
205222 annotations <- parseAnnotations c
@@ -230,14 +247,29 @@ parsePlainContent c = do
230247 }
231248
232249parseSimpleContent :: Cursor -> P Xsd.SimpleContent
233−parseSimpleContent c = parseError c "not implemented"
250+parseSimpleContent c = do
251+ restrictionAxis <- makeElemAxis "restriction"
252+ extenstionAxis <- makeElemAxis "extension"
253+ case (c $/ restrictionAxis, c $/ extenstionAxis) of
254+ ([r], []) -> Xsd.SimpleContentRestriction <$> parseSimpleRestriction r
255+ ([], [e]) -> Xsd.SimpleContentExtension <$> parseSimpleExtension e
256+ _ -> parseError c "Expected one of restriction or extension"
257+
258+parseSimpleExtension :: Cursor -> P Xsd.SimpleExtension
259+parseSimpleExtension c = handleNamespaces c $ do
260+ base <- theAttribute "base" c >>= makeQName c
261+ attributes <- parseAttributes c
262+ return Xsd.SimpleExtension
263+ { Xsd.simpleExtensionBase = base
264+ , Xsd.simpleExtensionAttributes = attributes
265+ }
234266
235267parseComplexContent :: Cursor -> P Xsd.ComplexContent
236268parseComplexContent c = do
237269 restrictionAxis <- makeElemAxis "restriction"
238270 extenstionAxis <- makeElemAxis "extension"
239271 case (c $/ restrictionAxis, c $/ extenstionAxis) of
240− ([r], []) -> parseError r "not implemented"
272+ ([r], []) -> Xsd.ComplexContentRestriction <$> parseComplexRestriction r
241273 ([], [e]) -> Xsd.ComplexContentExtension <$> parseComplexExtension e
242274 _ -> parseError c "Expected one of restriction or extension"
243275
@@ -252,6 +284,17 @@ parseComplexExtension c = handleNamespaces c $ do
252284 , Xsd.complexExtensionAttributes = attributes
253285 }
254286
287+parseComplexRestriction :: Cursor -> P Xsd.ComplexRestriction
288+parseComplexRestriction c = handleNamespaces c $ do
289+ base <- theAttribute "base" c >>= makeQName c
290+ attributes <- parseAttributes c
291+ model <- parseModelGroup c
292+ return Xsd.ComplexRestriction
293+ { Xsd.complexRestrictionBase = base
294+ , Xsd.complexRestrictionModel = model
295+ , Xsd.complexRestrictionAttributes = attributes
296+ }
297+
255298parseModelGroup :: Cursor -> P (Maybe Xsd.ModelGroup)
256299parseModelGroup c = do
257300 sequenceAxis <- makeElemAxis "sequence"
@@ -274,10 +317,10 @@ parseAll :: Cursor -> P Xsd.ModelGroup
274317parseAll c = Xsd.All <$> parseElements c
275318
276319-- | Get all child elements
277−parseElements :: Cursor -> P [Xsd.Element]
320+parseElements :: Cursor -> P [Xsd.RefOr Xsd.Element]
278321parseElements c = do
279322 elementAxis <- makeElemAxis "element"
280− mapM parseElement (c $/ elementAxis)
323+ mapM parseElementOrRef (c $/ elementAxis)
281324
282325parseAttributes :: Cursor -> P [Xsd.Attribute]
283326parseAttributes c = do
@@ -286,24 +329,33 @@ parseAttributes c = do
286329
287330parseAttribute :: Cursor -> P Xsd.Attribute
288331parseAttribute c = do
289− name <- theAttribute "name" c >>= makeTargetQName
290332 use <- parseUseAttribute c
291−
292− -- XXX: generalaze RefOr parsing
293− tp <- case anAttribute "type" c of
294− Nothing -> do
295− simpleTypeAxis <- makeElemAxis "simpleType"
296− case c $/ simpleTypeAxis of
297− [s] -> Xsd.Inline <$> parseSimpleType s
298− [] -> parseError c "Attribute should have type"
299− _ -> parseError c "Multiple simple types"
300− Just t -> Xsd.Ref <$> makeQName c t
301−
302− return Xsd.Attribute
303− { Xsd.attrName = name
304− , Xsd.attrUse = use
305− , Xsd.attrType = tp
306− }
333+ case (anAttribute "name" c, anAttribute "ref" c) of
334+ (Just n, Nothing) -> do
335+ name <- makeTargetQName n
336+ tp <- case anAttribute "type" c of
337+ Nothing -> do
338+ simpleTypeAxis <- makeElemAxis "simpleType"
339+ case c $/ simpleTypeAxis of
340+ [s] -> Xsd.Inline <$> parseSimpleType s
341+ [] -> return (Xsd.Ref anySimpleType)
342+ _ -> parseError c "Multiple simple types"
343+ Just t -> Xsd.Ref <$> makeQName c t
344+ return $ Xsd.InlineAttribute $ Xsd.AttributeInline
345+ { Xsd.attributeInlineName = name
346+ , Xsd.attributeInlineUse = use
347+ , Xsd.attributeInlineType = tp
348+ }
349+ (Nothing, Just r) -> do
350+ ref <- makeQName c r
351+ return $ Xsd.RefAttribute $ Xsd.AttributeRef
352+ { Xsd.attributeRefRef = ref
353+ , Xsd.attributeRefUse = use
354+ }
355+ _ -> parseError c "Should be either name or ref"
356+ where
357+ anySimpleType =
358+ Xsd.QName (Just (Xsd.Namespace Xsd.schemaNamespace)) "anySimpleType"
307359
308360parseUseAttribute :: Cursor -> P Xsd.Use
309361parseUseAttribute c = case anAttribute "use" c of