Quickstart With XML: Part 2

This part provides simple syntax rules to write a XML document. It is a complete XML document.



You can see that there are two kinds of information as shown in the preceding figure. Markup like <mods> and the text, or character data, personal and Kernighan, Brian W. The following diagram depicts the syntax rules to write various types of markup and text in an XML document.



Let's us see each component of the preceding diagram in detail.

XML declaration

XML documents can contain an XML declaration that is present, must be the first to construct the document. An XML declaration is made up of as many as three name/value pairs, syntactically identical to attributes. The three attributes have a compulsory version attribute and optional encoding and standalone attributes. The order of these attributes within an XML declaration is fixed. The XML declaration is not considered to be a processing instruction.

Example



Tags & Elements

A markup construct tags that begins with < and ends with >. Tags come in three types.

Start-tags

For example: <section>

End-tags

For example: </section>

empty-element tags; for example: <line-break />

A logical document component either begins with a start-tag and ends with a matching end-tag or consists of only an empty-element tag. The characters between the start-tags and end-tags, are the element's content and may contain markup, including other elements that are called child elements.

Example

  1. <Greeting>Hello, world.</Greeting>  
Element Syntax

Each XML element must be closed either with a start or with an element as shown.

<element>……..</element>

Root Element

In any markup language, the first element to appear is called the "root element", that defines what kind of document the file will be. In an HTML file, the <html> tag is the root element. An HTML file will always have the HTML element as the root element, whereas in an XML file, it can be anything.

<x>……</x> , <y>…..</y>

The following is an example showing the correct form in a XML document:

<root>
<x>……</x>
<y>…..</y>
</root>


Attribute

A markup construct consists of a name/value pair that exists within a start-tag or empty-element tag. In the example (below) the element img has two attributes, src and alt:
  1. <img src="shresth.jpg" alt='Foligno Madonna, by Raphael' />  
Another example would be:
  1. <step number="3">Connect A to B.</step>  
An XML attribute can only have a single value and each attribute can appear at most once on each element. In the common situation where a list of multiple values is desired, this must be done by encoding the list into a well-formed XML attribute with some format beyond what XML defines itself. Usually this is either a comma or semi-colon delimited list or, if the individual values are known not to contain spaces.
  1. <div class="inner greeting-box" >Hello!</div>  
where the attribute "class" has both the value "inner greeting-box" and also indicates the two CSS class names "inner" and "greeting-box".

XML Text

The names of XML elements and XML attributes are case sensitive.

Up Next
    Ebook Download
    View all
    Learn
    View all