Advertise Here
Need an introduction to XML? Learn what XML is all about and discover how XML differs from HTML. Explore XML syntax rules, learn how to write well-formed XML documents, adjust XML attributes, validate XML documents, and more. - Web05
Extensible Markup Language

Search XML Files:

XML Files RSS Feed XML Files Updates

 

DEVELOPER CHANNEL
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

Find a Web Host With:
CGI Capabilities
Active Server Pages
Windows Hosting
Unix Hosting
Dedicated Servers
     
 Advanced Search
 Be a Commerce Partner















IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

XML Attributes

by Jan Egil Refsnes

back next

XML Attributes

XML attributes are normally used to describe XML elements, or to provide additional information about elements. From HTML you can remember this construct: <IMG SRC="computer.gif">. In this HTML example SRC is an attribute to the IMG element. The SRC attribute provides additional information about the element.

Attributes are always contained within the start tag of an element. Here are some examples:

HTML examples:

<img src="computer.gif">
<a href="demo.asp">
XML examples:

<file type="gif">
<person id="3344">

Usually, or most common, attributes are used to provide information that is not a part of the content of the XML document. Did you understand that? Here is another way to express that: Often attribute data is more important to the XML parser than to the reader. Did you understand it now? Anyway, in the example above, the person id is a counter value that is irrelevant to the reader, but important to software that wants to manipulate the person element. 


Use of Elements vs. Attributes

Take a look at these examples:

Using an Attribute for sex:

<person sex="female">
  <firstname>Anna</firstname>
  <lastname>Smith</lastname>
</person>

Using an Element for sex:

<person>
  <sex>female</sex>
  <firstname>Anna</firstname>
  <lastname>Smith</lastname>
</person>

In the first example sex is an attribute. In the last example sex is an element. Both examples provides the same information to the reader.

There are no fixed rules about when to use attributes to describe data, and when to use elements. My experience is however; that attributes are handy in HTML, but in XML you should try to avoid them, as long as the same information can be expressed using elements.

Here is another example, demonstrating how elements can be used instead of attributes. The following three XML documents contain exactly the same information. A date attribute is used in the first, a date element is used in the second, and an expanded date element is used in the third:

<?xml version="1.0"?>
<note date="12/11/99">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note> 
<?xml version="1.0"?>
<note>
<date>12/11/99</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note> 
<?xml version="1.0"?>
<note>
<date>
  <day>12</day>
  <month>11</month>
  <year>99</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note> 


Avoid using attributes? (I say yes!)

Why should you avoid using attributes? Should you just take my word for it? These are some of the problems using attributes:

  • attributes can not contain multiple values (elements can)
  • attributes are not expandable (for future changes)
  • attributes can not describe structures (like child elements can)
  • attributes are more difficult to manipulate by program code
  • attribute values are not easy to test against a DTD

If you start using attributes as containers for XML data, you might end up with documents that are both difficult to maintain and to manipulate. What I'm trying to say is that you should use elements to describe your data. Use attributes only to provide information that is not relevant to the reader. Please don't end up like this:

<?xml version="1.0"?>
<note day="12" month="11" year="99"
to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note> 

This don't look much like XML. Got the point?


An Exception to my Attribute rule

Rules always have exceptions. My rule about not using attributes has one too:

Sometimes I assign ID references to elements in my XML documents. These ID references can be used to access XML element in much the same way as the NAME or ID attributes in HTML. This example demonstrates this:

<?xml version="1.0"?>
<messages>
  <note ID="501">
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
  </note>

  <note ID="502">
    <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.
back next
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 DevX XML Content 
- Why Data as a Service Will Reshape EAI
- Transitioning from XSLT 1.0 to 2.0, Part 2
- Transition from XSLT 1.0 to XSLT 2.0, Part 1
- Simplify Your Apps with the XML Binding Language 2.0
- Creating Custom Export Filters for StarOffice with XSLT

Jump to : Top Of Page or HOME



The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers