Xml 如何重复使用相同的<;xs:choice>;在不同的复杂类型定义中?
我正在为描述文章、论文、书籍等章节的XML文件编写模式。高级概念是Xml 如何重复使用相同的<;xs:choice>;在不同的复杂类型定义中?,xml,xsd,Xml,Xsd,我正在为描述文章、论文、书籍等章节的XML文件编写模式。高级概念是可以有任意数量的段落、节、图像和列表。现在,同样适用于一个部分,它也可以有任意数量的段落、图像、列表;子部分也可以(尚未实现) 我当前的架构如下所示: <xs:complexType name="chapter-type"> <xs:choice maxOccurs="unbounded"> <xs:element name="section" type="section-type" /&
可以有任意数量的段落
、节
、图像和列表。现在,同样适用于一个部分,它也可以有任意数量的段落、图像、列表;子部分也可以(尚未实现)
我当前的架构如下所示:
<xs:complexType name="chapter-type">
<xs:choice maxOccurs="unbounded">
<xs:element name="section" type="section-type" />
<xs:element name="par" type="par-type" />
<xs:element name="image" type="image-type" />
<xs:element name="ol" type="list-type" />
<xs:element name="ul" type="list-type" />
</xs:choice>
</xs:complexType>
<xs:complexType name="section-type">
<xs:choice maxOccurs="unbounded">
<xs:element name="par" type="par-type" />
<xs:element name="image" type="image-type" />
<xs:element name="ol" type="list-type" />
<xs:element name="ul" type="list-type" />
</xs:choice>
</xs:complexType>
<!-- <subsection> and other content-containing elements will repeat the par, image, ol, ul -->
正如你所看到的,有很多重复,在我想重用章节内容的小节和其他地方,情况会变得“更糟”
我可以添加一个新元素,比如
或其他什么,来包装段落/图像/列表,但这需要我将该元素添加到XML中。这就是我想要避免的
因此,我的问题是:如何避免到处重复这些元素?使用命名组
<xs:group name="paragraphs-etc">
<xs:choice>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="par" type="par-type" />
<xs:element name="image" type="image-type" />
<xs:element name="ol" type="list-type" />
<xs:element name="ul" type="list-type" />
</xs:choice>
</xs:choice>
</xs:group>
然后参考复杂类型中的组:
<xs:complexType name="chapter-type">
<xs:choice maxOccurs="unbounded">
<xs:element name="section" type="section-type" />
<xs:group ref="paragraphs-etc"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="section-type">
<xs:group ref="paragraphs-etc"/>
</xs:complexType>
组引用的重复信息来自组引用,而不是组定义。(因此,将段落etc组包装在一个不必要的xs:choice中——它确保了对该组的任何引用都是对一组可重复的选项的引用。)非常有效,谢谢!我注意到我需要在组的第二个嵌套
xs:choice
中使用minOccurs=“0”maxOccurs=“unbounded”
,否则我的XML将无法验证。