Wednesday, September 18, 2024

Creating the XML Document

This tutorial is divided into two parts. We will explain how to create an XML document and how to assign data types to the values in the XML document. All of the examples in this tutorial are designed to be viewed with either Internet Explorer 5.0 or 5.5.

Creating the XML Document and the Schema

Historically, the HyperText Markup Language (HTML) has been the coding technology used to publish content on the World Wide Web. Developed twenty years ago, HTML is a very easy to use language that allows you to create a static page composed of text, images, and hyperlinks that can be viewed by anyone in the world who has access to the Internet. Today, the integration of HTML with other technologies, such as Active Server Pages (ASP) and SQL, is common place and permits the creation of dynamic web sites that interact with the user. The future promises to bring a fantastic offering of new technologies that are expected to profoundly effect how we develop web sites.

One of the emerging technologies that is currently receiving considerable attention is the eXtensible Markup Language (XML). Both HTML and XML are markup languages, which means that they use embedded tags to dictate how a document is to be displayed. HTML is composed of a static number of pre-defined tags which have specific tasks. For example, the HTML <b> tag renders the text in
a bold font style. In contrast, you can use XML to create an unlimited number of your own custom tags and attributes, assign a data type to each tag and attribute, access values associated with the tags, and accomplish all of this in a custom structured format that you have also created.

The XML custom tags themselves have no meaning and you should not think that you are creating custom HTML tags. It is important to understand that the sole purpose of these XML tags is to contain data. It is the value assigned to the tag, and not the tag, that is important.

The ability to create custom tags to specifically meet the needs of your web pages can open many interesting possibilities for the site developer.

Tags

The first step is to create your own custom tags. This requires that we obey the syntax rules for XML in terms of both creating the individual tags and placing them in a logical order. An XML document that obeys the syntax rules is said to be well-formed. If the rules are not obeyed, you will get error messages. Fortunately, these rules are very simple.

Your tags can be composed of almost any string of characters, with certain limitations:

The first character of an XML tag must be an upper or lower case letter, the underscore, or a colon. The remaining characters can be composed of any combination of upper or lower case letters,
colons, hyphens, numbers, periods, or underscores. XML tags are case sensitive, i.e., <Fish> and <fish> are two different tags. All of your custom XML tags must be closed. There are two ways to close a tag legally. The first way is just like in HTML. Note how
the opening tag <fish> and the closing tag </fish> delimit (enclose) the text, which is neon tetra. The text is referred to as the value associated with the <fish> tag.

<fish>neon tetra</fish>

The second way, which is a blank space followed by a backslash, is used when there is no value associated with the tag.

<fish />

A set of opened and closed tags is commonly referred to as an element. You can nest XML elements inside other elements, but only in a logical manner. The inner nested tags must be closed before you close the outer nested tags.

Here are two examples of nested XML custom tags:

<aquarium> <fish>neon tetra</fish> </aquarium>

In the following example, we add one more layer of nested tags and repeat the <fish> element. Note how both the <name> and <number> tags are closed before we close the <fish> element in which they are contained.

<aquarium> <fish> <name>angel fish</name> <number>9</number>
</fish>
<fish>
<name>discus</name> <number>16</number>
</fish>
<fish>
<name>neon tetra</name>
<number>76</number>
</fish>
</aquarium>

Attributes

Just as an HTML tag can have attributes, you can also create one or more custom XML attributes for your own custom XML tags. You can use these attributes to provide additional information associated with your tag. The attribute is a name/value pair separated by an equal sign. The value must be enclosed by a pair of single or double quotes.

To demonstrate the use of attributes, we create a list of DevGuru staff members. Note how the programmer tag is used as a container element for the name, dob (date of birth), and ssn (social security number) tags for each staff member. In this list, each employee is assigned an id number (empID) as an attribute of the <programmer> tag, and the age (age) is listed as an attribute of the <dob> tag.

<devguru_staff> <programmer empID=”1″> <name>Bugs Bunny</name> <dob age=”30″>03/21/1970</dob> <ssn>111-11-1111</ssn> </programmer> <programmer empID=”2″> <name>Daisy Duck</name> <dob age=”51″>08/09/1949</dob> <ssn>222-22-2222</ssn> </programmer> <programmer empID=”4″> <name>Minnie Mouse</name> <dob age=”23″>04/13/1977</dob> <ssn>333-33-3333</ssn> </programmer> <programmer empID=3″> <name>Pluto</name> <dob age=”21″>07/04/1979</dob> <ssn>444-44-4444</ssn> </programmer> <programmer empID=”6″> <name>Porky Pig</name> <dob age=”43″>11/30/1956</dob> <ssn>555-55-5555</ssn> </programmer> <programmer empID=”5″> <name>Road Runner</name> <dob age=”47″>01/19/1953</dob> <ssn>666-66-6666</ssn> </programmer> </devguru_staff>

Comments

<!– You can comment XML code in this manner –>

Schema

The next major step is to create a schema which is a formal way of defining and validating the content of an XML document. (A well-formed XML document that conforms to its schema is said to be valid.)

The schema is how we assign the data types to each tag and any attributes that are contained in the XML document. A schema is a structured document which must obey XML syntax rules. It is composed of a series of predefined tags and attributes that are part of the XML language and are used to set the data types for the values associated with our custom tags. Simply put, not only do we get to create custom XML tags, but we can also denote that an XML data value is, for example, an integer data type. This ability to assign specfic data types to specific XML data values is one of the reasons why XML has attracted so much attention….

To read the rest of this tutorial, click: http://www.devguru.com/ then click on tutorials in the left nav bar and then click on: A Beginners Guide to Creating and Displaying Your First XML Document

DevGuru.com is a developer’s resource featuring comprehensive quick references for current technologies, free online tutorials, and “ask DevGuru,” the place to get your most difficult programming questions answered from leading experts around the world. Check
them out at http://www.devguru.com

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles