0

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?

dingalapadum
  • 2,077
  • 2
  • 21
  • 31

1 Answers1

1

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>
Aydin K.
  • 3,309
  • 36
  • 44
  • Thanks for your answer! Actually it goes quite in the direction I expected and although it makes sense for me what you say, just for the sake of completeness: Do I understand correctly, that in the first variant I could use ```` to reuse the element? Would there be a difference if in the complexType I use the ```` version vs using the ````one? If you add some words about this in your answer I'll happily accept and upvote. Thanx! – dingalapadum May 20 '20 at 12:19
  • I updated the answer with an example and a suggestion (xsd2xml converter). – Aydin K. May 20 '20 at 12:41
  • Thank you very much for the explanation. I must add to this, that I wasn't expecting the last line (with ``nsA``) like that... – dingalapadum May 20 '20 at 12:47
  • Hey! I was running this stuff in the xsd validator on the same page you linked (https://www.liquid-technologies.com/online-xsd-validator) and funnily enough the validation fails.... – dingalapadum May 20 '20 at 12:59
  • Yeah you're right :D Seems that the tooling is not 100% accurate. But for getting a picture of the relationships between XSD definitions and their XML results the page I linked should be sufficient – Aydin K. May 20 '20 at 13:18