| |
Naming rules for Element Tags and Attribute Names
Element tag names and Attribute names can begin with a letter, a colon, or an underscore, but NOT with numbers.
-
Subsequent characters may include alphanumeric, underscores, hyphens, and colons.
-
Names cannot contain certain XML-specific symbols like the ampersand (&), the "at" symbol (@), or the "less-than" symbol (<).
-
Element tag names and Attribute names cannot contain white space.
- Names cannot contain parenthetic statements ( words enclosed in parentheses or brackets ).
Which has a better usage, element or attribute ?
The classic XML design question, asked by adopters is when to use elements and attributes in XML design. Elements are used to encapsulate pieces of data, and attributes are generally used to provide accompanying information about an element. So, given below are the benefits and disadvantages of both elements and attributes and these can be used as a guiding principle for using elements and attributes :
Benefits of Using Elements
They are more extensible because attributes can later be added to them without affecting a processing application.
-
They can contain other elements.
-
They can be repeated.
-
You have more control over the rules of their appearance. For example, you can say that a product can either have a number or a product code child. This is not possible for attributes.
- Their order is significant if specified as part of a sequence, while the order of attributes is not. Obviously, this is only an advantage if you care about the order.
- When the values are lengthy, elements tend to be more readable than attributes.
Disadvantages of Using Elements
Elements require start and end tags, so are therefore more verbose.
| Note |
| Not all elements require a start and end tag - elements can be declared in a single line. |
Benefits of Using Attributes
They are less verbose.
Attributes can be added to the instance by specifying default values.
Attributes are atomic and cannot be extended and its existence should serve to remove any and all possible ambiguity of the element it describes. Disadvantages of Using Attributes
Attributes may not be extended by adding children, whereas a complex element may be extended by adding additional child elements.
If attributes are to be used in addition to elements for conveying business data, rules are required for specifying when a specific data item shall be an element or an attribute.
| |
| Summary |
| |
In this chapter you have learnt:
- About the importance of XML elements and attributes.
- What are various rules to be followed while naming attributes and elements?
- What is better to use, amongst element and attribute?
- Various benefits and drawbacks of using elements.
- Benefits and drawbacks of using attributes.
|
| |
| Review Questions |
| |
Fill in the Blanks
- The opening tag for an element is also called as ------- tag.
- <SmithBaker> will end with the -------- tag.
- </Tim> is an ---------- element.-------
- provide additional information for an element.
- Element tag names can begin with a ---------, a colon or an ---------.
Solutions
- start
- </SmithBaker>
- empty
- Attributes
- letter, underscore
Which of the following are valid declarations?
- <Liza> She is a banker </liza>
- <switch><bulb>Second switch from the right, on the switch board, is for bulb.</switch></bulb>
- </Switch>
- <Student class = "fifth'> Rosa </Student>
- <Bird type= 'non-flying'> Penguin </Bird>
Solutions
- Invalid. The start tag does not match with the end tag. XML is case-sensitive.
- Invalid. The overlapping of tags is not allowed in XML.
- Valid. This is an empty tag.
- Invalid. The quotes, in which the value of the attribute is given, does not match with each other.
- Valid.
|
| |
| What's Next |
The next chapter will acquaint you with the basic concepts of XML schema and a DTD. The chapter will further elucidate the types of DTD's and how a document is validated.
Hop over to the next chapter to get an XML schema and DTD close-up.
|
| |
|
| |
|