To constrain an XML file with a schema:
Take the Computer.xml file as an example.
1. Create a computer.xml file.
<?xml version= "1.0" encoding= "UTF-8"?>
<computers
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns= "Http://www.example.org/computer"
xsi:schemalocation= "Http://www.example.org/computer computer.xsd"
>
<computer>
<memories>
<memory>
<size>1G</size>
</memory>
<memory>
<size>2G</size>
</memory>
</memories>
</computer>
</computers>
1.2. Create a computer.xsd file.
<?xml version= "1.0" encoding= "UTF-8"?>
<schema xmlns= "Http://www.w3.org/2001/XMLSchema"
Targetnamespace= "Http://www.example.org/computer"
Xmlns:tns= "Http://www.example.org/computer"
Xmlns:mem= "Http://www.example.org/memory" elementformdefault= "qualified"
>
<element name= "Computers" >
<complexType>
<sequence>
<element name= "Computer" type= "Tns:computertype" >
</sequence>
</complexType>
</element>
<complextype name= "ComputerType" >
<all>
<element name= "Memories" >
<complexType>
<sequence>
<element name= "Memory" minoccurs= "1" maxoccurs= "2" ></element>
</sequence>
</complexType>
</element>
</all>
</complexType>
</schema>
2. Create a computer.xml file.
<?xml version= "1.0" encoding= "UTF-8"?>
<computers
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns= "Http://www.example.org/computer"
Xmlns:mem= "Http://www.example.org/memory"
xsi:schemalocation= "Http://www.example.org/computer computer.xsd
Http://www.example.org/memory memory.xsd "
>
<!--the following two sentences need to be added during the nesting process.
Xmlns:mem= "Http://www.example.org/memory"
xsi:schemalocation= "Http://www.example.org/memory memory.xsd"
-
<computer>
<memories>
<memory>
<mem:size>1G</mem:size>
</memory>
<memory>
<mem:size>2G</mem:size>
</memory>
</memories>
</computer>
</computers>
2.2. Create a computer.xsd file.
<?xml version= "1.0" encoding= "UTF-8"?> <schema xmlns= "Http://www.w3.org/2001/XMLSchema" targetnamespace= " Http://www.example.org/computer "
Xmlns:tns= "Http://www.example.org/computer"
Xmlns:mem= "Http://www.example.org/memory"
elementformdefault= "qualified" >
<!--add a couple of sentences to the nesting process.
Xmlns:mem= "Http://www.example.org/memory"
<import namespace= "Http://www.example.org/memory" schemalocation= "Memory.xsd" ></import>
-
<import namespace= "Http://www.example.org/memory" schemalocation= "Memory.xsd" ></import>
<element name= "Computers" >
<complexType>
<sequence>
<element name= "Computer" type= "Tns:computertype" ></element>
</sequence>
</complexType>
</element>
<complextype name= "ComputerType" >
<all>
<element name= "Memories" >
<complexType>
<sequence>
<element name= "Memory" type= "Mem:memorytype" minoccurs= "1" maxoccurs= "2" >
</element>
</sequence>
</complexType>
</element>
</all>
</complexType>
</schema>
2.3 Create a memory.xsd file.
<?xml version= "1.0" encoding= "UTF-8"?>
<schema xmlns= "Http://www.w3.org/2001/XMLSchema"
Targetnamespace= "Http://www.example.org/memory"
Xmlns:tns= "Http://www.example.org/memory"
elementformdefault= "qualified" >
<complextype name= "Memorytype" >
<all>
<element name= "Size" type= "string" ></element>
</all>
</complexType>
</schema>
Java uses schema constraint XML file