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.
我一直纠结于如何创建和XSD,以便“对象”节点的子节点可以是“文本”或“图像”节点,可以以任意的次数和顺序显示。它们在“对象”节点中出现的顺序决定了它们的呈现方式,但不需要对顺序进行验证。
Example 1
示例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
示例2
<objects>
<imageobject x="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!
谢谢!
3 个解决方案
#1
1
You could use xs:choice
with minOccurs="0"
and maxOccurs="unbounded"
:
可以使用xs:choice with minOccurs="0", 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>
#2
3
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.
在这种情况下,使用替换组可能是合适的。将“mediaObject”定义为一个抽象元素,使用“textObject”和“imageObject”作为其替换组的成员,然后将内容模型定义为
#3
1
Use a <xs:choice maxOccurs="unbounded">
.
使用一个< xs:选择maxOccurs = " unbounded " >。
#1
1
You could use xs:choice
with minOccurs="0"
and maxOccurs="unbounded"
:
可以使用xs:choice with minOccurs="0", 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>
#2
3
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.
在这种情况下,使用替换组可能是合适的。将“mediaObject”定义为一个抽象元素,使用“textObject”和“imageObject”作为其替换组的成员,然后将内容模型定义为
#3
1
Use a <xs:choice maxOccurs="unbounded">
.
使用一个< xs:选择maxOccurs = " unbounded " >。