我正在创建XSD架构,我需要定义混合的复杂型元素(既具有文本和元素),但是我需要要求这些元素是非空的。对于SimpleType,有最小的长度,但我不知道该如何(或者如果可能的话)将其限制为混合的复杂类型。
为了说明需求,以以下示例,其中有一个"文本" root元素,而混合的复杂类型为"名称":
"<text></text>" <= Valid
"<text>His name was <name>John <unk/> Mal<del>c</del>kovich</name>.</text>" <= Valid
"<text>His name was <name>John Malkovich</name>.</text>" <= Valid
"<text>A person called <name></name> was outside.</text>" <= Invalid
现在,我有以下架构对最后一个示例有效,但我需要它是无效的。如何要求TextType是非空的?
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<!--==============-->
<!-- ROOT ELEMENT -->
<!--==============-->
<xs:element name="text">
<xs:complexType mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:group ref="TextTypeChoice"/>
<xs:element name="name" type="NameType" maxOccurs="unbounded"/>
</xs:choice>
</xs:complexType>
</xs:element>
<!--==============================-->
<!-- Group for Text type elements -->
<!--==============================-->
<xs:group name="TextTypeChoice">
<xs:choice>
<xs:element name="unk" type="EmptyType" maxOccurs="unbounded"/>
<xs:element name="del" type="RawTextType" maxOccurs="unbounded"/>
</xs:choice>
</xs:group>
<!--=============================-->
<!-- Definition of the Text type -->
<!--=============================-->
<xs:complexType name="TextType" mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:group ref="TextTypeChoice"/>
</xs:choice>
</xs:complexType><!--TextType-->
<!--=============================-->
<!-- Definition of the Name type -->
<!--=============================-->
<xs:complexType name="NameType">
<xs:complexContent>
<xs:extension base="TextType"/>
</xs:complexContent>
</xs:complexType><!--NameType-->
<!--===================-->
<!-- Type for raw text -->
<!--===================-->
<xs:simpleType name="RawTextType">
<xs:restriction base="xs:token">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType><!--RawTextType-->
<!--========================================-->
<!-- Type for empty / self-closing elements -->
<!--========================================-->
<xs:simpleType name="EmptyType">
<xs:restriction base="xs:string">
<xs:maxLength value="0"/>
</xs:restriction>
</xs:simpleType><!--EmptyType-->
</xs:schema>
选项
-
在 XSD 1.0 中,您可以通过进行一些调整来支持您的模型:
-
有一个单独的类型,用于
text
root元素的内容模型允许它具有混合内容,可能是空的,其中包括name
,del
和unk
。 -
全球(不仅在
text
之内)禁止name
,del
和unk
无空。
-
-
在 XSD 1.1 中,您可以通过断言可以强制迫使
text
中发生不同约束的断言,而不是全球存在:<xs:assert test="every $e in .//* satisfies $e != ''"/>