XML Schema
Computer with Visual Studio
XML Schema
1. Launch the Visual Studio and click on File > New > File menu item.
2.Choose XML File in the dialog box and click open. An editor tab with name ending in [Design] will appear. You are displayed few options to create/edit schema. Click on "Use the XML Editor to view and edit the underlying XML schema file".
3.Save the file as wonders.xsd in working directory.
4.Review the XML schema file. A root level xs:schema tag is already created. Review the various attributes in the tag.
5.Add following code inside the xs:schema tags:
< xs:element name="wonder">
< xs:complexType>
< xs:sequence>
< xs:element name="name">
< xs:complexType>
< xs:simpleContent>
< xs:extension base="xs:string">
< xs:attribute name="language" type="xs:string"/>
< /xs:extension>
< /xs:simpleContent>
< /xs:complexType>
< /xs:element>
< xs:element name="location" type="xs:string"/>
< xs:element name="height" type="xs:string"/>
< /xs:sequence>
< /xs:complexType>
< /xs:element>
Notes: The xs:element with name wonders defines our root level xml element. All other tags from our XML file will be defined as children of this tag. With xs:complexType tag, we are indicating that this element is a complex type element which can contains other elements and/or attributes. With xs:sequence tag, we are indicating that all enclosing tags should appear in the given sequence and can have any number of occurrences (including 0 i.e. may be missing). The name element is a complexType since we want to add language attribute to it. The other two elements - location and height are simple type since they do not have any attributes or children.
6.Define an element name < history> within the < wonder> element sequence.
7.Define < year_built> and < year_destroyed> elements of type integer.
8.Define the < how_destroyed> element as type string. Set the default to fire.
9.We can declare custom types outside the root tag. These custom types can be then used to create multiple elements. Add the following code as second child of xs:schema tag
< xs:simpleType name="story_type">
< xs:restriction base="xs:string">
< xs:length value="1024"/>
< /xs:restriction>
< /xs:simpleType>
10.Define element named < story> of story_type as child of the < history> element.
11.Define a simple type named year_type that is a whole number and has a minimum value of 0 and a maximum value of 5000.
12.Modify the < year_built> and < year_destroyed> elements to be of type year_type.
13.In the wonders.xsd [Design] tab, click on "Use the XML Schema Explorer to browse your entire schema as a tree". In the Schema Explorer window expand and review your schema.
1.Based on the wonders.xml, complete your schema wonders.xsd.
2.Convert year_type to a complex type. It should be defined as a positive integer that has an attribute named era of type string.