XML Schema 复合类型 - 混合内容学习笔记

什么是混合内容?

在 XML 中,元素可以包含文本和其他元素。混合内容指的是元素中既包含了文本,又包含了其他元素。

如何定义混合内容的 XML 元素?

XML Schema 通过 complexType 元素的 mixed 属性来指定一个元素是否允许混合内容。如果 mixed 属性设为 true,则表示该元素允许混合内容。

Copy Code
<xs:element name="example" type="xs:string" mixed="true"/>

混合内容的实例

下面是一个元素包含混合内容的实例。该元素为 article,包含了标题、作者、时间和正文四个子元素,其中正文部分还包含了段落和强调标签。

Copy Code
<xs:complexType name="articleType"> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string"/> <xs:element name="time" type="xs:date"/> <xs:element name="content" mixed="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="paragraph" type="xs:string"/> <xs:element name="emphasis" type="xs:string"/> </xs:choice> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType>

在上述示例中,content 元素被定义为混合内容,因此其子元素既可以是文本,也可以是 paragraph 或 emphasis 元素。至于最终的文档格式,则由解析器根据实际情况进行解析和生成。