简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM