开发者

Content restriction and attribute validation on the same element in XSD

开发者 https://www.devze.com 2023-04-06 12:23 出处:网络
I would like to validate that an element \'Test\' should Have its content restricted (for example, using a pattern restriction), and

I would like to validate that an element 'Test' should

  • Have its content restricted (for example, using a pattern restriction), and
  • Contain certain attributes (for example, 'id', 'class' and 'name').

The 开发者_运维知识库XSD I'm writing looks like this:

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

However, when I code this in Visual Studio, I get the following error on the 'xsd:attribute' elements:

'attribute' and content model are mutually exclusive

Is there a way to validate both a content restriction and attributes on the same element?


You need to separate out your restriction and give it a name, then refer to it as a base type for an extension. Like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号