XML Tutorial
XML is an acronymn EXtensible Markup Language. It was designed to structure, store and transfer data. XML reperesents a data schema in the sense that it describes data. XML in itself does not do anything. It is a set of standards for structuring data and as such programs and people can understand the data and use it in a meaningful way.
Structured information contains both content (words, pictures, etc.) and some indication of what role that content plays (for example, content in a heading has a different meaning and emphasis than content in a footnote)
Almost all documents have some structure. We can do more with documents that adhere to a standard and are well structured.
In order to appreciate XML, it is important to understand why it was created. "Originally designed to meet the challenges of large-scale electronic publishing, XML is also playing an increasingly important role in the exchange of a wide variety of data on the Web and elsewhere." (extract from w3.org/XML/)
HTML on the other hand was designed to present and display data. Use XML to say what your data is and HTML / CSS to display your data in a presentable format and with relevant links. You can store data in a separate XML file and then use HTML to display the data.
Lets consider some basic examples. The document below in blue is an exmple of an XML format
<?xml version="1.0"?>
<tutorial>
<about>XML</about>
<author type="company" >RDS</author>
<location type="internet" >http://www.rds.co.za/</location>
<comment>This is a very useful tutorial about XML </comment>
<rating>****</rating>
</tutorial>
XML uses tags to structure the data. There are no pre-defined tags. You create your own thereby making XML extensible i.e. it can be extended.
The tags used to markup HTML documents and the structure of HTML documents are predefined. The author of HTML documents can only use tags that are defined in the HTML standard. XML on the other hand, allows you to define your own tags and your own document structure.
Lets look at some of the XML rules:
- Attribute values must always be quoted
- With XML, white space is preserved
- The syntax for writing comments in XML is similar to that of HTML.
- <!-- This is a comment -->
- XML Elements are Extensible
- Elements are related as parents and children.
- Elements can have different content types.
- XML elements must follow these naming rules
- Names can contain letters, numbers, and other characters
- Names must not start with a number or punctuation character
- Names must not start with the letters xml (or XML, or Xml, etc)
- Names cannot contain spaces
Any name can be used, no words are reserved, but the idea is to make names descriptive. Names with an underscore separator are descriptive.
Examples: <first_name>, <last_name>.
Avoid "-" and "." in names. For example, if you name something "capital-city," it could be a mess if a program tries to subtract city from capital . When you name something "first.name," some program may think that "name" is a property of the object "first."
XML elements can have attributes.
Attribute values must always be enclosed in quotes, but either single or double quotes can be used.
XML attributes are normally used to describe XML elements, or to provide additional information about elements
Examples:
<ebook version="2.0">
<file type="jpg">
<product id="A921">
Use of Elements vs. Attributes
Using an Attribute to indicate a persons sex:
<person sex="female">
<firstname>Natasha</firstname>
<lastname>Banks</lastname>
</person>
Using an Element for sex:
<person><sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
In the first example sex is an attribute. In the last example sex is an element. Both examples provides the same information to the reader. However the second example is clearer.
There are no fixed rules about when to use attributes to describe data, and when to use elements. Mostly i prefer to use elements and try to avoid attributes in XML
Here is another example, demonstrating how elements can be used instead of attributes. The following three XML documents contain exactly the same information. A date attribute is used in the first, a date element is used in the second, and an expanded date element is used in the third:
<?xml version="1.0"?>
<note date="2006/05/22">
<to>Michael</to>
<from>Lisa</from>
<heading>Reminder</heading>
<body>Don't forget Mom's birthday tomorrow!</body>
</note>
<?xml version="1.0"?>
<note>
<date>2006/05/22</date>
<to>Michael</to>
<from>Lisa</from>
<heading>Reminder</heading>
<body>Don't forget Mom's birthday tomorrow!</body>
</note>
<?xml version="1.0"?>
<note> <date>
<day>22</day>
<month>05</month>
<year>2006</year>
</date>
<to>Michael</to>
<from>Lisa</from>
<heading>Reminder</heading>
<body>Don't forget Mom's birthday tomorrow!</body>
</note>
The following are some reasons why you should use elements rather than attributes to describe data
- attributes can not contain multiple values (elements can)
- attributes are not expandable (for future changes)
- attributes can not describe structures (like child elements can)
- attributes are more difficult to manipulate by program code
- attribute values are not easy to test against a DTD (Document Type Definition)
Use attributes for things sundry information such as version numbers and ID codes.
The above has been a brief introduction to XML. Next we move on to formatting XML documents with style sheets
XML formatting examples
Have not found what you looking for, try searching Google. Simply use the search box below: