About Me

Friday, 13 April 2012

XML Tags

XML Tags

As was mentioned quickly in the XML Element Lesson, XML tags signify the opening and closing of an element. The two types of tags are:

  • Opening Tag - <element>
  • Closing Tag - </element>
If you already have a knowledge of how tag works from HTML or other experience, then you can skip this tag lesson.

A Tag Pair

The opening and closing tags are referred to as a tag pair. All data that belongs to a specific element will be appearing within this tag pair.

The Solo Tag

In addition to the standard tag pair, there are elements that contain no text. This means the tag pair can be substituted with a modified opening tag. You may have seen these types of tags in HTML before.
  • Modified opening tag
  • - <element />
Notice that there is a slash after the name of the element. This acts as a signal to the XML processor that this element has completed and is a shorthand for elements that have no data between opening and closing tags.

The Tag Ordering Rule

Every XML tag must be closed in the order that it was opened. If an XML tag was opened inside another element, then it must be closed before the outer element can close. If this rule is not obeyed, an XML processing error will occur. Below are examples of good and bad XML documents. Try to figure out which are valid XML and which violate a rule.

XML Code 1:

<email>
  <to>A. Nony Mouse</to>
 <body>Hey There!</body>
</email>

XML Code 2:

<email>
 <to>A. Nony Mouse
 <from>Bobby</to></from>
  <body>Hey There!</body>
</email>

XML Code 3:

<email>
  <to>A. Nony Mouse</to>
 <from>Bobby</from>
 <body>Hey There!</body>
</email>
The correct XML documents are the first and third examples. The second example does not follow the ordering requirement, as the "to" element is closed before the embedded "from" element". This problem was fixed in the third example by moving the "from" element out of the "to" element.

Indenting and Spacing XML Tags

The error in the above example was rather easy to pick out due to our indentation and spacing. A good rule of thumb is to start a new line before placing a new tag. Also, if a tag is nested, then indent to make this fact clearer.

0 comments:

Post a Comment