kossmoboleat
kossmoboleat

Reputation: 1951

XML Schema: define type of types

I'd like to define a schema that contains elements specifying an XML schema type. This question may be related to the XML Schema for schemas and this question.

Here's what I have so far:

<xs:complexType name="metatype">
    <xs:sequence>
      <xs:element name="datatype" type="datatype" minOccurs="0" maxOccurs="1"/>
      <xs:element name="location" type="locationtype" minOccurs="0"maxOccurs="unbounded"/>
      <xs:element name="alias" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:ID" use="required"/>
    <xs:attribute name="editable" type="xs:boolean" default="false" use="optional"/>
    <xs:attribute name="forcedvisible" type="xs:boolean" default="false" use="optional"/>
</xs:complexType>

where datatype is:

<xs:complexType name="datatype">
    <xs:sequence>
      <xs:element name="restriction">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="enumeration">
              <xs:complexType>
                <xs:attribute name="value" type="xs:string" use="required"/>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
          <xs:attribute name="base" type="xs:string" use="required"/>
          <!-- type xs:string is not accurate for XML Schema type -->
        </xs:complexType>
      </xs:element>
    </xs:sequence>
</xs:complexType>

Instead of declaring datatype I'd like to use the localSimpleType in the Schema for schemas or at least the simpleRestrictionType but my XML Schema editor (Visual Studio) does not seem to recognize these types. Is there another XML Schema document that I need to reference? I'd really like to avoid defining the whole XML Schema simpleType element and its subtags for restrictions, etc..

Upvotes: 3

Views: 3692

Answers (2)

Michael Kay
Michael Kay

Reputation: 163585

I think you should be able to write a schema that imports the S4S and references types defined in it. It's entirely possible that some tools will object, however.

One thing you shouldn't do is try to process a modified S4S, or to add extra components into the XSD namespace. Schema-aware tools are entitled to treat everything in the S4S as axiomatic, and presenting them with definitions that differ from their built-in knowledge of these components could cause untold havoc.

Upvotes: 1

Tony Hopkinson
Tony Hopkinson

Reputation: 20330

Do you want to use xsd or invent your own and write your own parser as well?

You can define types with or without restrictions, or you can add a local restriction to a defined type in a known namespace which usually includes xs.

Local restrictions are best for one offs, if you use it more than once, define a new type for DRY (donot repeat yourself)

e.g.

<xs:simpleType name="mySimpleTypeA">
  <xs:restriction base = "xs:string">
    <xs:enumeration value ="on"/>
    <xs:enumeration value = "off"/>
  </xs:restriction>
</xs:simpleType>

and

<xs:complexType name="myComplexTypeA>
  <xs:sequence>
    <xs:element name="someElementName">
      <xs:simpleType>
        <xs:restriction base ="xs:string">
          <xs:minLength ="8"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
  </xs:sequence>
  <xs:attribute name="someAttributeName" use="required" type="mySimpleTypeA"/>
</xs:complexType>

Then you can use them as in

<xs:element name = "Fred" type ="myComplexTypeA"/>

calling your element restriction, isn't going to make it one...

Oh off the top of my head this, didn't validate it, but it should be close.

Upvotes: 1

Related Questions