All in one forum  - Applications | Games | E-Books | Music, Movies & Videos | Mobile Stuff | Live Discussions | Webmaster Stuff | Many More | Community to Hang Out & Stick to
Mark Forums Read

Go Back   Home > Tutorial Section > Programming > CSS and XML
Reload this Page XML Attributes
Forgot Password? Join Us!
CSS and XML Post your Cascading Style Sheets and eXtensible Markup Language related tutorial here

Notices
Free Image Hosting Solution for all Your link here @ 10$ 10 K Unique Visitors 1 Million Visitors Per Month Your link here @ 10$

Your Ad Here

Rate This Thread - XML Attributes.

Post New Thread Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
 
KoOL's Avatar
 
User Info
Join Date: Oct 2007
Location: In You Mind
Age: 27
Achievements Posts: 10,857
Casino Cash: $1148680

Total Points: 238,104,053,100.91
Donate

KoOL has disabled reputation

Awards Showcase
Administrator Of the Month Of Jan 
Total Awards: 1
XML Attributes - 01-10-2008, 07:57 AM

XML elements can have attributes in the start tag, just like HTML.
Attributes are used to provide additional information about elements.
XML Attributes

XML elements can have attributes.
From HTML you will remember this: <IMG SRC="computer.gif">. The SRC attribute provides additional information about the IMG element.
In HTML (and in XML) attributes provide additional information about elements:
<img src="computer.gif">
<a href="demo.asp"> Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:
<file type="gif">computer.gif</file>
Quote Styles, "female" or 'female'?

Attribute values must always be enclosed in quotes, but either single or double quotes can be used. For a person's sex, the person tag can be written like this:
<person sex="female"> or like this:
<person sex='female'> Note: If the attribute value itself contains double quotes it is necessary to use single quotes, like in this example:
<gangster name='George "Shotgun" Ziegler'> Note: If the attribute value itself contains single quotes it is necessary to use double quotes, like in this example:
<gangster name="George 'Shotgun' Ziegler">
Use of Elements vs. Attributes

Data can be stored in child elements or in attributes.
Take a look at these examples:
<person sex="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
<person>
<sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person> In the first example sex is an attribute. In the last, sex is a child element. Both examples provide the same information.
There are no rules about when to use attributes, and when to use child elements. My experience is that attributes are handy in HTML, but in XML you should try to avoid them. Use child elements if the information feels like data.
My Favorite Way

I like to store data in child elements.
The following three XML documents contain exactly the same information:
A date attribute is used in the first example:
<note date="12/11/2002">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note> A date element is used in the second example:
<note>
<date>12/11/2002</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note> An expanded date element is used in the third: (THIS IS MY FAVORITE):
<note>
<date>
<day>12</day>
<month>11</month>
<year>2002</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Avoid using attributes?

Should you avoid using attributes?
Some of the problems with using attributes are:
  • attributes cannot contain multiple values (child elements can)
  • attributes are not easily expandable (for future changes)
  • attributes cannot describe structures (child elements can)
  • attributes are more difficult to manipulate by program code
  • attribute values are not easy to test against a Document Type Definition (DTD) - which is used to define the legal elements of an XML document
If you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data.
Don't end up like this (this is not how XML should be used):
<note day="12" month="11" year="2002"
to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>
An Exception to my Attribute Rule

Rules always have exceptions.
My rule about attributes has one exception:
Sometimes I assign ID references to elements. These ID references can be used to access XML elements in much the same way as the NAME or ID attributes in HTML. This example demonstrates this:
<messages>
<note id="p501">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

<note id="p502">
<to>Jani</to>
<from>Tove</from>
<heading>Re: Reminder</heading>
<body>I will not!</body>
</note>
</messages> The ID in these examples is just a counter, or a unique identifier, to identify the different notes in the XML file, and not a part of the note data.
What I am trying to say here is that metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.



For Download Links U need to Press Thanks Button

Please don't PM me For Posts, Request and Not Working Threads. Use Button.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following 2 Users Say Thank You to KoOL For This Useful Post:
barnick (06-28-2008), Muhammad_Omar (04-09-2008)
Click here to Donate to remove the Adverts.
Your Ad Here
Post New Thread Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


New To AiO Forum? Need Help?

All times are GMT. The time now is 10:06 PM.
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles

RapidShare Links PhazeDDL Warez
PhazeDDL Warez