At some point, you need to use a reference to one of the elements you have already defined with a name, for example by using a named complex type.
Within each element, instead of using <xs:all>
, use <xs:choice>
. By doing so, you can make sure that either another level or the string element is the child element at any level.
This could look similar to the following (untested, as I don't see your complete XSD in the question, but you get the idea):
<xs:element name="TextData" type="myTextDataType"/>
<xs:complexType name="myTextDataType">
<xs:choice>
<xs:element name="TextGroup" type="myTextDataType"/>
<xs:element name="Text" type="xs:string"/>
</xs:choice>
</xs:complexType>
Or, explained in different words:
It's the same "trick" as when building a chain of something in formal grammars. Within the chain, you can choose between a rule that prolongs the chain by adding another non-terminal, "looping back to the current state from where the chain can be further prolonged" (say, A -> aA
), and a rule that ends the chain with something irregular (for example, A -> b
).