Alex Rivera | Logout

What is the difference between XML and XSD?

Asked 2010-02-25T12:43:43.010
125

What is the difference between Extensible Markup Language (XML) and XML Schema Definition (XSD)?

Edit
Report

1 Answer

122

Take an example

<root>
  <parent>
     <child_one>Y</child_one>
     <child_two>12</child_two>
  </parent>
</root>

And design an XSD for that:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" 
xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="parent">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="child_one" type="xs:string" />
              <xs:element name="child_two" type="xs:int" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

What isn't possible with XSD: would like to write it first as the list is very small

  1. You can't validate a node/attribute using the value of another node/attribute.
  2. This is a restriction: An element defined in XSD file must be defined with only one datatype. [in the above example, for <child_two> appearing in another <parent> node, the datatype cannot be defined other than int]
  3. You can't ignore the validation of elements and attributes, ie, if an element/attribute appears in XML, it must be well-defined in the corresponding XSD. Though usage of <xsd:any> allows it, but it has got its own rules. Abiding which leads to the validation error. I had tried for a similar approach, and certainly wasn't successful, here is the Q&A

What are possible with XSD:

answered 2010-02-26T05:24:26.637

Your Answer