| |
| Overview |
| |
| In this chapter you will learn : |
- What are XML elements and attributes?
- How to name an element tag and an attribute?
- Which one is more efficient to use, amongst attribute and element?
- What are the advantages and disadvantages of using both?
|
XML Elements
Elements are considered to be the basic building blocks of any mark up language. Each element has its own specific properties and functions. XML's element mark up is composed of 3 parts :
A start tag.
-
The contents.
-
The end tag.
-
Each element begins and ends with its element name ( usually referred to as a tag) .
-
Each element begins with what is commonly called a start tag or an opening tag .
-
Generally, each element ends with an end tag also called as a closing tag .
-
Each tag name is delimited by angle brackets ( < indicates the beginning of the tag name; > indicates its end ) .
-
In the end tag, the element name is always preceded by a slash ( / ) .
-
Every start-tag must have a matching end-tag.
-
Tags cannot overlap. Proper nesting is required.
-
The start tag and end tag should be treated like wrappers, and whenever you think of an element, you should have a mental picture of a piece of text with both tags in place.
| Syntax |
| < element name > ..... content ....... < /element name > |
| Example |
<P> This is content of paragraph. </P>
Here, <P> indicates the start tag and </P> indicates the end tag and the content lies in between these two tags. |
| Note |
| The exception to this rule is the declared empty element( already discussed in previous chapter), which does not have an end tag, but does have a special start tag. |
XML Attributes
Elements may or may not include attributes (also called attribute specifications). They are type of data that you can specify for your elements, and they appear in the form of name-value pairs.
-
Attributes are used to attach information to the information contained in an element.
-
The general form for using an attribute is :
| Syntax |
| < element.name property = " value " > |
| Symbol |
Description |
| < |
Start tag open delimiter |
| element.name |
Element name |
| property |
Attribute name |
| = |
Value indicator |
| " |
Literal string delimiter |
| ' |
Alternative literal string delimiter |
| value |
Value of the attribute |
| > |
Start tag close delimiter |
-
An attribute value must be enclosed in quotation marks. You can use either single quote (< fruit size = 'big'>) or double quotes (<fruit smell = "pungent">), But you cannot mix the two in the same specifications.
-
When you specify attributes for the same element more than once, the specifications are simply merged.
| Example |
< !-- Element with attributes -->
< ?xml version = "1.0" >
<My.page>
<student rollno = "first" > Student has roll number 1.</student>
<student rollno = 'second' remarks = 'obedient' > Student is obedient. </student>
</My.page> |
|