Search code examples
xmlxsdxsd-validationxml-validation

How to define a local type in XSD?


How to define type inside the element in XML Schema rather than referencing in the element?

 <xs:element name="Payment" type="my:Payment"/>

But I want to do something like below...

<xs:element name="Payment">
  <type="my:Payment"/>
</element>

But getting syntax error.


Solution

  • In XSD, you can reference a globally-defined1 type as you show in your first example,

    <xs:element name="Payment" type="my:Payment"/>
    

    or use a locally-defined, anonymous type, not like you show,

    <xs:element name="Payment">
      <type="my:Payment"/>
    </element>
    

    but rather, for example,

    <xs:element name="Payment">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="Amount"/>
          <xs:element name="Date"/>
        </xs:sequence>
      </xs:complexType>
    </element>
    

    Note that such locally defined types are anonymous and cannot be reused.

    See also


    1 See How to reference global types in XSD? for a detailed example using namespaces.