xmlweb-servicesxsdschemaxmlschema

simpleType definitions xml schema


I am a bit unsure about the different examples I'm seeing on how simpleTypes are declared/defined. From what I see on both sites the syntax description is the same, but the examples differ.

In https://www.w3schools.com/xml/el_simpletype.asp I see:

<xs:element name="age">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="100"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

But in https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256050(v%3Dvs.100) they use:

<xs:simpleType name="freezeboilrangeInteger">
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="0"/>
    <xs:maxInclusive value="100"/>
  </xs:restriction>
</xs:simpleType>

Are these 2 ways different? If so, what is the difference? If they are the same, which one shoud be used /is best practice?


Solution

  • The first example is an xsd-element which contains its type definition in an embedded way.

    The second example contains just a type definition (xs:simpleType) without an element which is referencing it. But a type definition without an element or attribute referencing it makes no really sense.

    Therefore, to make both examples comparable (having same result), you need to add an element in the second example which references the type definition:

    <xs:element name="age" type="tns:freezeboilrangeInteger"/>
    
    <xs:simpleType name="freezeboilrangeInteger">
      <xs:restriction base="xs:integer">
        <xs:minInclusive value="0"/>
        <xs:maxInclusive value="100"/>
      </xs:restriction>
    </xs:simpleType>
    

    From both options (embedded or external type definition) the latter one is preferable because of being reusable.

    //Update (due to the comment)

    I've setup a XSD here, which contains those three approaches (embedded type definition, reference type definition and referenced element), because I think playing around online with such xsd2xml generator is the best way to learn about XSD. Starting point is mysequence:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="mytns" xmlns:tns="mytns">
    
        <xs:element name="agetest">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:minInclusive value="0" />
                    <xs:maxInclusive value="100" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
    
        <xs:simpleType name="freezeboilrangeInteger">
            <xs:restriction base="xs:integer">
                <xs:minInclusive value="0" />
                <xs:maxInclusive value="100" />
            </xs:restriction>
        </xs:simpleType>
    
        <xs:element name="mysequence">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="age">
                        <xs:simpleType>
                            <xs:restriction base="xs:integer">
                                <xs:minInclusive value="0" />
                                <xs:maxInclusive value="100" />
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:element>
                    <xs:element name="age2" type="tns:freezeboilrangeInteger" />
                    <xs:element name="age3" ref="tns:agetest" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    

    which generates following sample XML result:

    <?xml version="1.0" encoding="utf-8"?>
    <mysequence xmlns="mytns">
      <age>83</age>
      <age2>86</age2>
      <nsA:agetest xmlns:nsA="mytns">18</nsA:agetest>
    </mysequence>