Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content restriction and attribute validation on the same element in XSD

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?

like image 916
Jonathan Avatar asked Sep 21 '11 01:09

Jonathan


1 Answers

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>
like image 188
devuxer Avatar answered Oct 22 '22 17:10

devuxer