XSD How To?

A reference to a DTD or an XML Schema is possible for the XML documents.

A Simple XML Document:

Note.xml:

<?xml version="1.0"?>
<note>
  <to>Sapna</to>
  <from>Tom</from>
  <heading>Message</heading>
  <body>Meeting on Monday at 11 AM.</body>
</note>

A DTD File:

Note.dtd:

<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

Explanation:

We are using the above DTD file to define the elements of the XML document note.xml. The four child elements: “to, from, heading, body” of the note element is defined in the first line. The type of the child elements: to, from, heading, body elements are defined as “#PCDATA”, in the line 2-5.

An XML Schema:

Note.xsd:

<?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:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="to" type="xs:string"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
 
</xs:schema>

Explanation:

In the above example, we are using an XML Schema file to define the elements of the XML document “note.xml”. The note element contains other elements and is thus a complex type. The elements: to, from, heading, and body, do not contain other elements and are thus simple types.

A reference to a DTD:

A reference to a DTD is there for an XML document.

Example:

<?xml version="1.0"?>
 
<!DOCTYPE note SYSTEM
"https://www.w3spoint.com/xml/note.dtd">
 
<note>
  <to>Sapna</to>
  <from>Tom</from>
  <heading>Message</heading>
  <body>Meeting on Monday at 11 AM.</body>
</note>

A reference to an XML Schema:

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.w3spoint.com/xml note.xsd">
  <to>Sapna</to>
  <from>Tom</from>
  <heading>Message</heading>
  <body>Meeting on Monday at 11 AM.</body>
</note>
Please Share