Go to GoReading for breaking news, videos, and the latest top stories in world news, business, politics, health and pop culture.

How to Create an XML Schema Database

104 9
    • 1). Create an XML Schema to define a database of memos that you may send to employees. Do this by creating a file named "note.xsd" with any text editor.

    • 2). Define the XML version used in your schema. This should always be the first line in your XML document.

      <?xml version="1.0"?>

    • 3). Open the <schema> element and define your namespace. The schema element is the root element for all XML Schema documents and it must include all the information before being closed. Paste the following:

      <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

      </xs:schema>

      This opens the schema. The "xmlns" attribute defines the namespace for your schema. It is used to ensure that multiple XML documents that use the same names for data within them can keep things straight. In this case, you are using a namespace named "xsi" and setting it as the URL of the XML Schema standard at the W3C. Generally, the namespace should be your own webpage and should be set to a unique name. (see references 3)

    • 4). Paste the following code into your schema document, between the opening and closing the schema tags, like so:

      <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

      <xs:element name="note">
      <xs:complexType>
      <xs:sequence>
      <xs:element name="to" type="xs:string"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string"/>
      </xs:sequence>
      </xs:complexType>
      </xs:element>

      </xs:schema>

      (see references 1)

      This declares that any XML document using this schema should contain an element called "note." It then declares that notes are a complex type, meaning that it contains more than one piece of information.

      Next, the "sequence" tag is added. This declares that the data must appear in the XML document in the same order as they appear here. If you do not care about the order the data appears in XML, the "sequence" tag can be removed. (see references 4)

      Finally, the data of the "note" element are themselves listed. They are all listed as being of the string type. XML Schema provides dozens of data types for use in XML documents, however the major three are String, Date, and Numeric.

    • 5). Create a "note.xml" file with your text editor and paste the following.

      <?xml version="1.0"?>

      <note
      xmlns="http://www.w3schools.com"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.w3schools.com note.xsd">
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
      </note>

      As you can see, it declares that it is using the same namespace used in step 2, and provides a URL where the XML Schema document can be located by anyone viewing this XML document.

      Finally, it lists the data using the tags defined in the XML Schema: to, from, heading and body.

Source...

Leave A Reply

Your email address will not be published.