xml – 如何在不同的复杂类型定义中重用它?

我正在为 XML文件编写一个模式,描述文章,论文,书籍等各个章节.高级概念是< chapter>可以有任意数量的段落< par>,部分<部分>,图片和列表.现在,同样适用于一个部分,它也可以有任意数量的段落,图像,列表;小节也是如此(尚未实施).

我当前的架构看起来是这样的:

<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 -->

正如你所看到的,有很多重复,它会变得“更糟”的子章节和其他地方,我想重用章节/节的内容.

我可以添加一个新元素,比如< 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>

然后参考复杂类型中的组:

<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>

组引用的重复信息来自组引用,而不是组定义. (因此将paragraph-etc组包装在另外不必要的xs:choice中 – 它确保对组的任何引用都是对可重复选择集的引用.)

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念