如何定义一个非空的XSD混合complexType?

时间:2017-12-08 07:52:51

标签: xml xsd

我正在创建一个XSD架构,我需要定义混合的complexType元素(同时包含文本和元素),但我需要将它们设置为非空。对于simpleType,有minLength,但我不知道如何(或者如果可能)将此限制为混合complexType。

为了说明需要,请参考以下示例,其中有一个'text'根元素,混合complexType是'name':

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

1 个答案:

答案 0 :(得分:0)

选项

  1. XSD 1.0 中,如果我理解正确,您可以通过进行一些调整来支持您的模型:

    • 为其内容模型的text根元素设置单独的类型 允许它包含混合内容,可能是空的,包括 namedelunk

    • 全球(不仅仅在text内)禁止namedelunk为空。

  2. XSD 1.1 中,您可以通过断言支持您的模型,这些断言可以强制在text内发生不同的约束,而不是全局存在:

    <xs:assert test="every $e in .//* satisfies $e != ''"/>