XSD Schema Element

The root element of every XML Schema is the <schema> element.

The <schema> Element:

Syntax:

<?xml version="1.0"?>
 
<xs:schema>
...
...
</xs:schema>

The <schema> Element Attributes:

Some attributes can be included in the <schema> element.

Example: Schema declaration:

<?xml version="1.0"?>
 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3spoint.com"
xmlns="https://www.w3spoint.com"
elementFormDefault="qualified">
...
...
</xs:schema>

Explanation:

  • The xmlns:xs=”http://www.w3.org/2001/XMLSchema” fragment is used to specify that:
  • the elements and data types used in the schema come from the “http://www.w3.org/2001/XMLSchema” namespace.
  • the elements and data types that come from the “http://www.w3.org/2001/XMLSchema” namespace should be prefixed with xs.
  • The targetNamespace=”https://www.codesjava.com” fragment is used here to specify that the elements defined by the schema (note, to, from, heading, and body) come from the “https://www.codesjava.com” namespace.
  • The xmlns=”https://www.w3spoint.com” fragment is used here to specify that the default namespace is “https://www.w3spoint.com”.
  • The elementFormDefault= “qualified” fragment is used here to specify that any elements used by the XML instance document which were declared in this schema must be namespace-qualified.

Referencing a Schema in an XML Document:

A reference to an XML Schema is there for an XML document.

Example:

<?xml version="1.0"?>
 
<note xmlns="https://www.codesjava.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.codesjava.com note.xsd">
 
<to>Rose</to>
<from>Chris</from>
<heading>Message</heading>
<body>Meeting on Monday at 10 AM.</body>
</note>

Explanation:

  • The xmlns=”https://www.w3spoint.com” fragment is used here to specify that the default namespace is “https://www.w3spoint.com”, thus indicating the schema-validator that all the elements used in this XML document are declared in the “https://www.w3spoint.com” namespace.
  • We can use the schemaLocation attribute after the availability of the XML Schema Instance namespace xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”.
  • There are two values in this attribute, that are separated by a space. The namespace to use is defined by the first value of the attribute, while the location of the XML schema to use for that namespace is defined by the second value of the attribute, i.e., xsi:schemaLocation=”https://www.w3spoint.com note.xsd”.
Please Share