我正在为XML文件编写一个模式,描述文章,论文,书籍等各个章节.高级概念是a <chapter>可以包含任意数量的段落<par>,部分<section>,图像和列表.现在,同样适用于一个部分,它也可以有任意数量的段落,图像,列表; 小节也是如此(尚未实施).
我当前的架构看起来是这样的:
<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 -->
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,有很多重复,它会变得"更糟"的子章节和其他地方,我想重用章节/节的内容.
我可以添加一个新的元素,比方说<content>或其他什么来包装段落/图像/列表,但这需要我将该元素添加到我的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>
Run Code Online (Sandbox Code Playgroud)
然后参考复杂类型中的组:
<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>
Run Code Online (Sandbox Code Playgroud)
组引用的重复信息来自组引用,而不是组定义.(因此将paragraph-etc组包装在另外不必要的xs:choice中 - 它确保对组的任何引用都是对可重复选择集的引用.)
| 归档时间: |
|
| 查看次数: |
535 次 |
| 最近记录: |