43

What exactly is the difference between XML Schema Document and XML Schema Instance ?

  • xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

Please elaborate.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Piyush
  • 599
  • 1
  • 7
  • 16
  • 2
    `What exactly is the difference` - well, the exact difference is that they are different schemas that don't have common elements. A helpful link would be https://www.w3.org/TR/xmlschema-1/#Instance_Document_Constructions. – GSerg Dec 08 '16 at 08:58

2 Answers2

44

xsd and xsi Similarities

  • Both are XML namespace prefixes, abbreviations for an XML namespace.
  • Both are, as are all namespace prefixes, arbitrarily named; other namespace prefix abbreviations could equally well be used. However, both prefixes are conventional and therefore recommended. (An also-conventional alternative to xsd is xs.)

xsd and xsi Differences

See Also

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 2
    .NET's XmlSerializer seems to introduce these namespace prefixes even though (1) they aren't used in the document it produces, and (2) there is no schema to validate the document it produces. My takeaway is this is all by convention, which I suppose makes some sense, if not a whole lot of sense. – ChetPrickles Nov 03 '17 at 17:58
  • To include unused namespace prefix declarations of any sort is unnecessary but not wrong. Any serializer that includes unused namespace prefix declarations is simply not being as parsimonious as it could be. – kjhughes Feb 26 '18 at 13:33
  • 1
    Actually, the XmlSerializer uses the XMLSchemaInstance namespace to handle abstract classes, via the xsi:type attribute. It is also possible to generate schemas using XSD.exe. I've never seen the schemas embedded directly in the serialized XML file, but wouldn't surprise me if this was possible too. – glopes Aug 19 '19 at 14:30
  • There's a Schema Namespace, there's an Instance Namespace, so what's the use of the Default Namespace? – Minh Nghĩa May 23 '20 at 21:40
  • 1
    @MinhNghĩa: A *default namespace* is specified on an element via `xmlns="` *`default namespace URI`* `"` and places that element and its descendents in the given namespace, by default. A default namespace can be declared in any XML document, including even an XSD, where it can be used to avoid having to specify `xsd` or `xs` namespace prefixes on elements throughout the XSD. See also [Namespace related attributes in XML and XML Schema (XSD)](https://stackoverflow.com/q/34202967/290085) – kjhughes May 23 '20 at 21:58
6

http://www.w3.org/2001/XMLSchema

The Simple Version : This is the namespace used within an XML Schema (XSD). An XML schema is used to describe what's valid within an XML instance document.

The Less Simple Version : This is the namespace of an XML Schema that describes the structure of an XML Schema. In other words a schema that describes itself.

An XML Schema (XSD) must be written using the types defined within this schema.

For Example.

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="MyElement" type="xs:string" />
</xs:schema>

http://www.w3.org/2001/XMLSchema-instance

This is a namespace used within XML Instance documents to provide additional data to the XML Parser that is processing it. It describes the attributes xsi:schemalocation, xsi:noSchemalocation, xsi:type and xsi:nil which the XML parser can use to assist it with validation.

For Example.

<MyElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:noNamespaceSchemaLocation="MySchema.xsd">
    string
</MyElement>
Sprotty
  • 5,676
  • 3
  • 33
  • 52