开发者

XSD node with mixed content that appears any number of times in any order

I'm stuck on how to create and XSD that allows children of an 'objects' node be either 'text' or 'image' nodes any show up any number of times and in any order. The order in which they appear within the 'objects' node determines how they are rendered but the order does not need to be validated.

Example 1

<objects>
    <textobject x="30" y="100" value="blah1" />
    <imageobject x="0" y="0" src="/path/to/some/image1.png"/> 
    <imageobject x="0" y="0" src="/path/to/some/image2.png"/>
    <textobject x="60" y="250" value="blah2" />
    <textobject x="60" y="250" value="blah3" />
</objects>

Example 2

<objects> 
    <imageobject x开发者_Go百科="0" y="0" src="/path/to/some/image1.png"/>
    <textobject x="30" y="100" value="blah1" />
    <textobject x="60" y="250" value="blah2" />
    <imageobject x="0" y="0" src="/path/to/some/image2.png"/>
    <textobject x="60" y="250" value="blah3" />
</objects>

thanks!


In this situation it may well be appropriate to use a substitution group. Define "mediaObject" as an abstract element, with "textObject" and "imageObject" as members of its substitution group, and then define the content model as <xs:element ref="mediaObject" minOccurs="0" maxOccurs="unbounded"/>. The advantage of this design is that it's more extensible, it achieves separation of concerns, better expression of the semantics, and greater reusability of definitions. The benefits really start to show when there are 15 kinds of media object rather than two.


You could use xs:choice with minOccurs="0" and maxOccurs="unbounded":

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="objects">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="imageobject"/>
        <xs:element ref="textobject"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>
  <xs:element name="imageobject">
    <xs:complexType>
      <xs:attribute name="src" use="required"/>
      <xs:attribute name="x" use="required" type="xs:integer"/>
      <xs:attribute name="y" use="required" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="textobject">
    <xs:complexType>
      <xs:attribute name="value" use="required"/>
      <xs:attribute name="x" use="required" type="xs:integer"/>
      <xs:attribute name="y" use="required" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
</xs:schema>


Use a <xs:choice maxOccurs="unbounded">.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜