In: Computer Science
3.1. Create an XML schema to validate the following XML file.
<?xml version="1.0" encoding="utf-8"?>
<root>
<whats>Everything</whats>
<up>Is</up>
<doc>Fine</doc>
</root>
Schema starter:
<xsd:schema xmlns:xsd="">
<xsd:element name="root" type="rootType" />
<xsd:complexType name="rootType">
<xsd:sequence>
<xsd:element name="" type="" minOccurs="1" />
<xsd:element name="” type="" minOccurs="1" />
<xsd:element name="" type="" minOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
3.2. Use your schema to validate the XML file.
3.2.1. You can use Visual Studio or online utilities to apply the schema to the XML file.
3.2.2. You don’t know if the schema is being applied unless it gives you an error! Introduce an error into the XML file and verify that the error is caught by the schema.
For example: you might delete the <up> element. The validator should complain about the missing element.
3.3. When the schema catches an error, take a screenshot.
3.4. Revise the XML file to remove the error.
Answer :
XML scheme:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root" type="rootType" />
<xsd:complexType name="rootType">
<xsd:sequence>
<xsd:element name="whats" type="xsd:string" minOccurs="1" />
<xsd:element name="up" type="xsd:string" minOccurs="1" />
<xsd:element name="doc" type="xsd:string" minOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Explanation :
first line in the above xml scheme is used to declared namespace prefix we use xsd as namespance prefix.
Element : elements in the xml are the building blocks of XML document. in the second line of above xml scheme an element is defined having name as root:
<xsd:element name="root" type="rootType" />
here name is the name of element which is root here. and type is used to define type of element here it is set as root because it is root elemnt in given xml document
Some of the predefined simple types are: xsd:integer, xsd:boolean, xsd:string, xsd:date.
in the third line of xml scheme we have following line
<xsd:complexType name="rootType">
the above line is used to define complexType which means if root contain any other child element(s) then it will be used.
<xsd:sequence>
this element in xml scheme is used to define sequence of element(s) to be validated with given rule. rule will be define for each element inside above xml scheme tag.
<xsd:element name="whats" type="xsd:string" minOccurs="1" />
the above line validate element with name whats and it's data type should be string and this element should occurs at least on time in xml document. (inside element specified by complexType)
<xsd:element name="up" type="xsd:string" minOccurs="1" />
above line validate element having name up and it's type should be string and it should occurs at least once in document (inside element specified by complexType)
<xsd:element name="doc" type="xsd:string" minOccurs="1" />
above line validate element having name doc and it's type should be string and it should occurs at least once in document (inside element specified by complexType)
Validating with below XML document:
<?xml version="1.0" encoding="utf-8"?>
<root>
<whats>Everything</whats>
<up>Is</up>
<doc>Fine</doc>
</root>
Output:
Validating with below XML document:
<?xml version="1.0" encoding="utf-8"?>
<root>
<whats>Everything</whats>
<doc>Fine</doc>
</root>
Output:
in the above output we get an error stating that "up" element is expected because in we have removed "up" element from xml document.