Pages

Tuesday, May 22, 2012

XML Attributes and Elements


XML Attributes
XML elements can have attributes, just like HTML.
Attributes provide additional information about an element.
XML Attributes
In HTML, attributes provide additional information about elements:

<img src="screen.gif">

<a href="example.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 can be important to the software that wants to manipulate the element:

<file type="gif">screen.gif</file>

XML Attributes Must be Quoted

Attribute values must always be quoted.
 Either single or double quotes can be used. For a person's sex, the person element can be written like this:

<person sex="female">

or like this:

<person sex='female'>

If the attribute value itself contains double quotes you can use single quotes, like in this example:

<gangster name='Adam "Shotgun" Zuckenberg'>

or you can use character entities:

<gangster name="Adam &quot;Shotgun&quot; Zuckenberg">
XML Elements vs. Attributes
Take a look at these examples:
<person sex="female">
  <firstname>Angelina</firstname>
  <lastname>Will Smith</lastname>
</person>

<person>
  <sex>female</sex>
  <firstname>Angelina</firstname>
  <lastname>Will Smith</lastname>
</person>
In the first example sex is an attribute. In the last, sex is an element. Both examples provide the same information.

There are no rules about when to use attributes or when to use elements. Attributes are handy in HTML. 
In XML my advice is to avoid them. 
Use elements instead.
Best Way Practised
The following three XML documents contain exactly the same information:
A date attribute is used in the first example:
<note date="10/01/2008">
  <to>Tom</to>
  <from>Jerry</from>
  <heading>Reminder</heading>
  <body>Don't forget Birthday Party this weekend!</body>
</note>

A date element is used in the second example:
<note>
  <date>10/01/2008</date>
  <to>Tom</to>
  <from>Jerry</from>
  <heading>Reminder</heading>
  <body>Don't forget Birthday Party this weekend!</body>
</note>

An expanded date element is used in the third: (THIS IS MY FAVORITE):
<note>
  <date>
    <day>10</day>
    <month>01</month>
    <year>2008</year>
  </date>
  <to>Tom</to>
  <from>Jerry</from>
  <heading>Reminder</heading>
  <body>Don't forget Birthday Party this weekend!</body>
</note>

Thursday, May 17, 2012

XML Example


A lot of new Internet languages are created with XML.


Here are some examples:

  • XHTML .
  • WSDL for describing available web services.
  • WAP and WML as markup languages for handheld devices.
  • RSS languages for news feeds.
  • RDF and OWL for describing resources and ontology.
  • SMIL for describing multimedia for the web.

If developers have sense, future applications will exchange their data in XML since it is less time consuming and easily editable.

XML documents form a tree structure that starts at "the root" and branches to "the leaves".

An Example XML Document

XML documents use a self-describing and simple syntax:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
  <to>Peter</to>
  <from>Jani</from>
  <heading>Birthday Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

The first line is the XML declaration. It defines the XML version (1.0) and the encoding used (ISO-8859-1 = Latin-1/West European character set).

The next line describes the root element of the document (like saying: "this document is a note"):

<note>
The next 4 lines describe 4 child elements of the root (to, from, heading, and body):

<to>Peter</to>
<from>Jani</from>
<heading>Birthday Reminder</heading>
<body>Don't forget me this weekend!</body>
And finally the last line defines the end of the root element:

</note>
You can assume, from this example, that the XML document contains a note to Peter from Jani.

Don't you agree that XML is pretty self-descriptive?

  • XML Documents Form a Tree Structure
  • XML documents must contain a root element. This element is "the parent" of all other elements.
  • The elements in an XML document form a document tree. The tree starts at the root and branches to the lowest level of the tree.
  • All elements can have sub elements (child elements):

<root>
  <child>
    <subchild>.....</subchild>
  </child>
</root>

  • The terms parent, child, and sibling are used to describe the relationships between elements. Parent elements have children.
  • Children on the same level are called siblings (brothers or sisters).
  • All elements can have text content and attributes (just like in HTML).