XSD中相同元素的内容限制和属性验证

时间:2011-09-21 01:49:45

标签: attributes xsd restriction

我想验证元素'Test'应该

  • 限制其内容(例如,使用模式限制)和
  • 包含某些属性(例如,'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'元素上出现以下错误:

  

'属性'和内容模型是互斥的

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

1 个答案:

答案 0 :(得分:13)

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

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