[xsd-users] Sequence + Choice (or Alternative)

Boris Kolpackov boris at codesynthesis.com
Thu Apr 15 09:27:36 EDT 2010


Hi Marvin,

Marvin M. <xsd-users at groenndemon.de> writes:

> <grammar>
>     <rule lhs="A">
>         <non-terminal>Foo</non-terminal>
>         <terminal>Bar</terminal>
>         <string>Buzz</string>
>         <terminal>Bar</terminal>
>         <non-terminal>Foo</non-terminal>
>     </rule>
> </grammar>
> 
> [...]
> 
> However, I am unsure how to implement the rule's child nodes:
> There has to be at least one child node (valid elements are
> <non-terminal>, <terminal> and <string>), but there is no restriction on
> how many there could be.
> The order of the child nodes has to be preserved.

The following post explores this issue as well as the various solutions 
in great detail:

http://www.codesynthesis.com/pipermail/xsd-users/2010-March/002741.html


> I'm not that experienced with XML schemas yet, and I currently have a
> sequence of choices that would allow one of the three element types.
> But as the element types currently don't have a common base type, I
> think I can't use "--generate-polymorphic", can I?

If you want to use the substitutions group approach, then you will need
to create an abstract <rule-term> element and derive <non-terminal>, 
<terminal>, and <string> from it. Then, instead of using a sequence of 
choices, you will simply use the sequence of <rule-term> elements (which, 
in the actual XML document will be substituted with one of the concrete 
elements). The post mentioned above has an example schema fragment that 
shows how to do this.


> Anyway, I'd like to have something like an Item() method for my Rule
> class, so that I would be able to iterate through all children and check
> what element type they are.

Yes, you will be able to do this with the substitution group approach.

Boris



More information about the xsd-users mailing list