Basis of the XML Language

0

The elements of XML, like the elements of HTML, have a tag attributed at the beginning. The attributes will give supplementary information about the respective element

The XML attributes

You remember the <img src=”picture.gif”> attribute from HTML. The “src” attribute brings additional information about the <img> element.

In HTML, like in XML, the attributes are giving information about the elements that are not a part of data. In the next example, the type of the file is not important for data, but it is important for the application that will process the respective element:

<file type=”gif”>picture.gif</file>

 

The XML attributes need the quotation signs

The values of the attributes must always be quoted, but you can use simple or double quotes. If the value of the attribute has quotations, it must be put between simple quotation signs, but you can also use the quote signs for the elements contained by the afferent entities.

 

Elements vs. Attributes in the XML language

Pay attention to the next example:

<pupil sex=”feminine”>

<name>Carla</name>

<lastname>Drew</lastname>

</pupil>

 

<pupil>

<sex>feminine</sex>

<name>Carla</name>

<lastname>Drew</lastname>

</pupil>

 

it can be easily observed that the sex is an attribute in the first example. For the second example, it is only an element. Both examples would have the same result. There is no rule about using attributes or elements. A common practice would be to use attributes for HTML and the attributes for XML.

 

Is it recommended to avoid the attributes in XML?

A few problems that would be raised by the usage of attributes:

  • The attributes can’t contain more values, but the elements can.
  • The attributes can’t have a tree structure, while the elements can.
  • The attributes can’t be easily extended (for the next modifications)
  • The attributes are hard to read and maintain. It is recommended to use the elements for data storage. Use the attributes only for information that is not relevant for the data.

 

What is an XML element?

An XML element begins with a tag and ends with a tag. An element can contain other elements, simple texts, and also text combinations. The elements can also contain attributes.

 

<library>

<book category=”Programming”>

<title>The basis of PHP</title>

<author>Yourname</author>

<year>2011</year>

<price>40.00</price>

</book>

<book category=”Design”>

<title>Web template</title>

<author>Yourname</author>

<year>2010</year>

<price>60.00</price>

</book>

</library>

In this example, <library> and <book> have elements as content, <author> has a text content. In the same example, only the element <book> has an attribute (category=”Programming”)

Naming rules in XML

The name of the elements in XML must follow the next rules:

  • they can contain letters, numbers or other characters
  • they can’t begin with numbers or orthographic signs
  • the name can begin with the letters xml (XML, Xml, and so)
  • the names can’t contain spaces

 

Any name can be used, as none of the words is reserved. Try to use descriptive names. The elements containing underscores are great: <name_pupil>, <teacher_name>, etc.

The names must be short and simple like in the next example <name_book> instead of <name_of_the_borrowed_book>

Try to avoid the character “-“ like in the next example <name-pupil>. Some programs would think that you are trying to drop the “pupil” from the “name”.

Leave a Reply

Your email address will not be published. Required fields are marked *