Create XML file

I created a xml file with animals and their attributes.

animals.xml

<?xml version="1.0" encoding="UTF-8"?>
 
<animals
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:noNamespaceSchemaLocation="animals.xsd">
 
  <animal id="0001">
    <name>Cow</name>
    <weight>800KG</weight>
    <head>
      <eye>2</eye>
      <mouth>1</mouth>
      <nose>1</nose>
      <ear>2</ear>
    </head>
    <body>
      <fur>yes</fur>
    </body>
    <leg>4</leg>
  </animal>
 
  <animal id="0003">
    <name>Sheet</name>
    <weight>150KG</weight>
    <head>
      <eye>2</eye>
      <mouth>1</mouth>
      <nose>1</nose>
      <ear>2</ear>
    </head>
    <body>
      <fur>yes</fur>
    </body>
    <leg>4</leg>
  </animal>
 
  <animal id="0002">
    <name>Horse</name>
    <weight>500KG</weight>
    <head>
      <eye>2</eye>
      <mouth>1</mouth>
      <nose>1</nose>
      <ear>2</ear>
    </head>
    <body>
      <fur>yes</fur>
    </body>
    <leg>4</leg>
  </animal>
 
  <animal id="0010">
    <name>Pig</name>
    <weight>300KG</weight>
    <head>
      <eye>2</eye>
      <mouth>1</mouth>
      <nose>1</nose>
      <ear>2</ear>
    </head>
    <body>
      <fur>doubtful</fur>
    </body>
    <leg>4</leg>
  </animal>
 
  <animal id="0011">
    <name>Mockingbird</name>
    <weight>0.5KG</weight>
    <head>
      <eye>2</eye>
      <mouth>1</mouth>
      <nose>1</nose>
      <ear>2</ear>
    </head>
    <body>
      <fur>yes</fur>
    </body>
    <leg>2</leg>
  </animal>
 
  <animal id="0012">
    <name>Fur Seal</name>
    <weight>450KG</weight>
    <head>
      <eye>2</eye>
      <mouth>1</mouth>
      <nose>1</nose>
      <ear>2</ear>
    </head>
    <body>
      <fur>yes</fur>
    </body>
    <leg>0</leg>
  </animal>
 
  <animal id="0013">
    <name>Habor Seal</name>
    <weight>450KG</weight>
    <head>
      <eye>2</eye>
      <mouth>1</mouth>
      <nose>1</nose>
      <ear>0</ear>
    </head>
    <body>
      <fur>yes</fur>
    </body>
    <leg>0</leg>
  </animal>
</animals>

DTD file for animals

The XML file above will be validated against a DTD file. And the DTD file is defined below

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT animals (animal+)>
 
<!ELEMENT animal (name, weight, head, body, leg?)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT weight (#PCDATA)>
<!ELEMENT head (eye, mouth, nose, ear)>
<!ELEMENT eye (#PCDATA)>
<!ELEMENT mouth (#PCDATA)>
<!ELEMENT nose (#PCDATA)>
<!ELEMENT ear (#PCDATA)>
<!ELEMENT body (fur)>
<!ELEMENT fur (#PCDATA)>
<!ELEMENT leg (#PCDATA)>
 
<!ATTLIST animal id ID #REQUIRED>

Validate using DTD

This code reference the DTD file in the XML file.

<!DOCTYPE animals SYSTEM "animals.dtd">

XSD XML schema

Now I switch to XSD. Below is the XSD file I am going to use

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="animals">
<xs:complexType>
<xs:sequence>
    <xs:element name="animal" maxOccurs="unbounded">
 
        <xs:complexType>
 
            <xs:sequence>
                    <xs:element name="name" type="xs:string"/>
		    <xs:element name="weight" type="xs:string"/>
 
		    <xs:element name="head">
		        <xs:complexType>
                        <xs:sequence>
		            <xs:element name="eye" type="xs:integer"/>
		            <xs:element name="mouth" type="xs:integer"/>
		            <xs:element name="nose" type="xs:integer"/>
		            <xs:element name="ear" type="xs:integer"/>
		        </xs:sequence>
                        </xs:complexType>
		    </xs:element>
 
		    <xs:element name="body">
		        <xs:complexType>
                        <xs:sequence>
		            <xs:element name="fur" type="xs:string"/>
                        </xs:sequence>
		        </xs:complexType>
		    </xs:element>
 
		    <xs:element name="leg" type="xs:integer"/>
 
            </xs:sequence>
 
            <xs:attribute name="id" type="xs:string" use="required"/>
 
        </xs:complexType>
    </xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Use XSD file in XML

The syntax to link the xsd file to the xml file is

<animals
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:noNamespaceSchemaLocation="animals.xsd">

It is defined in the root element as attribute.

Validate the XSD itself

I use the w3c validate tool for the XSD.
http://www.w3.org/2001/03/webdata/xsv

It is successfully validated

Schema validating with XSV 3.1-1 of 2007/12/11 16:20:05

    * Target: file:/usr/local/XSV/xsvlog/tmp2yEmYsuploaded
         (Real name: animals.xsd)
    * docElt: {http://www.w3.org/2001/XMLSchema}schema
    * Validation was strict, starting with type [Anonymous]
    * The schema(s) used for schema-validation had
        no errors
    * No schema-validity problems were found in the target
       

validate the XML file with DTD

http://validator.w3.org/check
The above validator is used. In the first attempt it found 7 errors which are all of the same type

Line 21, Column 15: value of attribute "id" invalid: "0" cannot start a name

  <animal id="0001">

✉

It is possible that you violated the naming convention for this attribute. For example, id and name attributes must begin with a letter, not a digit.

.

I added “a” in front of the id value, i.e ie=“a0001”. Then it passed the check without any error

This document was successfully checked as XML!
Result: 	Passed, 1 warning(s)

And the only warning comes from the encoding. As I copy/paste the code directly into the online checker, it has to trust me with that the encoding is UTF-8 as I defined.