Question

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>...

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.

Solutions

Expert Solution

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.


Related Solutions

Create a XSD Schema to validate and provide structure for the XML document below: <?xml version="1.0"...
Create a XSD Schema to validate and provide structure for the XML document below: <?xml version="1.0" encoding="UTF-8" ?> <forecast qTime="28/10/20 10:00 PM" qLocation="Singapore"> <weather yyyymmdd="20200430"> <year>2020</year>   <month>04</month> <date>30</date> <comment>Plenty of sunshine</comment> <code>sunny</code> <highest>32.6</highest> <lowest>28.4</lowest> </weather> <weather yyyymmdd="20200218"> <year>2020</year>   <month>02</month> <date>18</date> <comment>Plenty of sunshine</comment> <code>sunny</code> <highest>34.6</highest> <lowest>30.5</lowest> </weather> <weather yyyymmdd="20200710"> <year>2020</year>   <month>07</month> <date>10</date> <comment>Partly sunny</comment> <code>partlySunny</code> <highest>33.1</highest> <lowest>29.2</lowest> </weather> <weather yyyymmdd="20200616"> <year>2020</year>   <month>06</month> <date>16</date> <comment>Considerable clouds</comment> <code>cloudy</code> <highest>30.5</highest> <lowest>25.4</lowest> </weather> <weather yyyymmdd="20200612"> <year>2020</year>   <month>06</month> <date>12</date> <comment>Cloudy with a thunderstorm</comment> <code>thunderstorm</code> <highest>29.1</highest>...
1. The following XML document describes some employees of a company. <?xml version="1.0" encoding="UTF-8"?> <company> <person...
1. The following XML document describes some employees of a company. <?xml version="1.0" encoding="UTF-8"?> <company> <person age="25" id="p29432" manager="p48293"> <ssn>123456789</ssn> <name> John Davis</name> <office>B432</office> <phone>1234</phone> </person> <person age="27" id="p29333" manager="p48222"> <ssn>987654321</ssn> <name>Jim Handler</name> <office>B123</office> </person> <person age=55 id="p29432" manager="p48221"> <ssn>123456789</ssn> <name> Christo Dichev</name> <office>A4210</office> <phone>2477</phone> </person> <person age="57" id="p29333" manager="p4832"> <ssn>987654321</ssn> <name>Elva Jones</name> <office>A4211</office> </person> </company> 1. Check if the XML document is well-formed. If it is not, change it so that it becomes wellformed, making as little changes as...
c# Create a console application that protects an XML file, such as the following example. Note...
c# Create a console application that protects an XML file, such as the following example. Note that the customer's credit card number and password are currently stored in clear text. The credit card must be encrypted so that it can be decrypted and used later, and the password must be salted and hashed: <?xml version="1.0" encoding="utf-8" ?> <customers> <customer> <name>Bob Smith</name> <creditcard>1234-5678-9012-3456</creditcard> <password>Pa$$w0rd</password> </customer> </customers>
Consider the following XML file: <root> <students> <element> <ID>100345</ID> <Nationality>USA</Nationality> <Program>ICT</Program> <age>23</age> <name>John</name> </element
Consider the following XML file: <root> <students> <element> <ID>100345</ID> <Nationality>USA</Nationality> <Program>ICT</Program> <age>23</age> <name>John</name> </element> <element> <ID>100876</ID> <Nationality>MALAYSIA</Nationality> <Program>CS</Program> <age>28</age> <name>Awang</name> </element> <element> <ID>100257</ID> <Nationality>AUSTRALIA</Nationality> <age>25</age> <name>Alex</name> </element> </students> </root Write an XQUERY to display the information for all students who are not Malaysians or older than 25.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT