liudaisuda
liudaisuda

Reputation: 59

XSD design of an xml element of dynamic sub-structure

I guess the word dynamic is misleading. The element of my interest could have three possible substructures. An example could be:

<body> 
    <date></date>
    <addr></addr>
</body>

or

<body>
    <loc></loc>
    <time> </time>
    <city> </city>
</body>

or

<body>
    //a whole lot of different sub elements
</body>

I am stuck in design the XSD that can be used to validate the three structures above (the three structures are known and fixed). I am not sure if I have put this question properly. Please shed any light on the solution, or even on the feasibility of it.

Upvotes: 0

Views: 199

Answers (2)

IndoKnight
IndoKnight

Reputation: 1864

You can define your <body> to contain arbitrary xml fragment using CDATA.

    <xs:element name="body" type="xs:CDATA"/>

More info, please refer http://www.w3.org/TR/2000/CR-xmlschema-2-20001024/#CDATA

Upvotes: 0

Petter
Petter

Reputation: 4165

Something like this would work I think:

<xs:group name="group_one">
  <xs:sequence>
    <xs:element name="date"/>
     <xs:element name="addr"/>
  </xs:sequence>
</xs:group>

<xs:group name="group_two">
  <xs:sequence>
    <xs:element name="loc"/>
    <xs:element name="time"/>
    <xs:element name="city"/>
  </xs:sequence>
</xs:group>

<xs:complexType name="body">
  <xs:choice>
    <xs:group ref="group_one" />
    <xs:group ref="group_two"/>
  </xs:choice>
</xs:complexType>

You can of course add more groups, and have more choices.

Upvotes: 2

Related Questions