XML - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

XML Attributes

XML Attributes

shape Description

An attribute provides single property of an element. It uses a name and value combination that are separated by equal "=" sign. Attribute provides extra data by defining the properties of XML elements. Syntax : [xml] <element-name attr1="attr1value" attr2="attr2value"> ....content.... < /element-name> [/xml] where, attribute1 and attribute2 follows the below representation: [xml]name = "value"[/xml] Example - 1 : [xml]<a href="http://www.splessons.com/">SPLessons</a>[/xml] In the above example, href is the name of attribute and http://www.splessons.com/ is the value of attribute. Example - 2 : [xml] <note> <date> <year>2008</year> <month>01</month> <day>10</day> </date> <to>Tove</to> <from>Jani</from> </note> [/xml]

Rules for using XML attributes

shape Description

1. An attribute's name must be an XML name. But when attaching attributes to an element, remember that no two attributes in one element can have the same name. 2. Attribute must be placed within the start tag only after the element name, after white space — no comma, semicolon etc. 3. Attribute values should be within single or double quotation marks. Single Quotes: [xml]<fruit name='apple'>[/xml] Double Quotes: [xml]<fruit name="apple">[/xml] If value has the quotes itself, then the representation can be of any of below two types: [xml]<fruit name="Green 'apple' "> or <fruit name='Green "apple"'>[/xml] 4. Attribute names should not be within quotation marks and are case sensitive. 5. An attribute's value cannot include a naked attribute delimiter and also cannot include a naked markup introducer. 6. An XML processor can deliver attributes to an application in any order. The authored order of attributes is irrelevant. The values could be passed to an application in alphabetic order by name, collation order by value, the same order as specified in an element's tag, perhaps even the reverse order in which they are specified. 7. Attributes cannot have multiple values and tree structures like elements and are not easily extensible.

Attributes to Metadata

shape Description

Elements sometimes are given with id's which helps to identify the same as HTML attribute id's. [xml] <college> <student id="350"> <name>Mike</name> <father>Murray</father> <place>Texas</place> </student> <student id="352"> <name>John</name> <father>Andy</father> <place>NewYork</place> </student> </college> [/xml] In the above example, id attributes helps to identify different students with their id's. Here metadata (data about data) have to be stored as attributes, and the data itself should be stored as elements.

Summary

shape Key Points

  • Attribute provides extra data by defining the properties of XML elements.
  • Attribute values should be within single or double quotation marks.