9

我想验证一个元素“测试”应该

  • 限制其内容(例如,使用模式限制),并且
  • 包含某些属性(例如,“id”、“class”和“name”)。

我正在编写的 XSD 如下所示:

<xsd:element name="Test" minOccurs="0" maxOccurs="unbounded">
  <xsd:complexType mixed="true">
    <xsd:simpleContent>
      <xsd:restriction>
        <xsd:pattern value="xyz"/>
      </xsd:restriction>
    </xsd:simpleContent>
    <xsd:attribute name="id" type="xsd:string"></xsd:attribute>
    <xsd:attribute name="class" type="xsd:string"></xsd:attribute>
    <xsd:attribute name="name" type="xsd:string"></xsd:attribute>
  </xsd:complexType>
</xsd:element>

但是,当我在 Visual Studio 中对此进行编码时,我在“xsd:attribute”元素上收到以下错误:

“属性”和内容模型是互斥的

有没有办法验证同一元素的内容限制和属性?

4

1 回答 1

14

您需要分离出您的限制并为其命名,然后将其称为扩展的基本类型。像这样:

  <xsd:simpleType name="RestrictedString">
    <xsd:restriction base="xsd:string">
      <xsd:pattern value="xyz" />
    </xsd:restriction>
  </xsd:simpleType>
  <xsd:element name="Test">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:extension base="RestrictedString">
          <xsd:attribute name="id" type="xsd:string" />
          <xsd:attribute name="class" type="xsd:string" />
          <xsd:attribute name="name" type="xsd:string" />
        </xsd:extension>
      </xsd:simpleContent>
    </xsd:complexType>
  </xsd:element>
于 2011-09-21T02:33:43.673 回答