Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Well, it's official: I'm thick. I have been programming years, but have never had to define an XML schema before and it is driving me up the wall. Despite looking at tens of examples, I just can't get the syntax right for defining an mandatory attribute on an element with the restriction that the attribute must be a number between 1 and 100.

I can't even get the following simpler example to work, which simply states the attribute must be a positive integer. I get an error saying that MyType2 is not declared or is not a simple type.

XML
<!-- Define a type to hold a single artwork. -->
    <xs:complexType name="ArtworkType">
        <xs:sequence>
            <xs:element name="title" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="ID" type="MyType2" use="required"/>
    </xs:complexType>

    <!-- Define attributes. -->
    <xs:attribute name="MyType2" type="xs:positiveInteger"/>


Please help before I go mad.

Kind wishes, Patrick
Posted

This should help you on your way
HTML
<xs:schema id="Alpha" xmlns:xs="#unknown">
    targetNamespace="http://tempuri.org/Alpha.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/Alpha.xsd"
    xmlns:mstns="http://tempuri.org/Alpha.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="Artwork">
    <xs:complextype>
      <xs:attribute name="ID" type="ArtworkType" />
    </xs:complextype>
  </xs:element>

  <xs:simpletype name="ArtworkType">
    <xs:restriction base="xs:integer">
      <xs:mininclusive value="0" />
      <xs:maxinclusive value="100" />
    </xs:restriction>
  </xs:simpletype>
</xs:schema>
 
Share this answer
 
v2
That's brilliant! Thank you.

- Patrick
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900