Monday 28 March 2011

Pengenalan XML

Extensible markup language

XML (Extensible Markup Language) adalah bahasa markup untuk keperluan umum yang disarankan oleh W3C untuk membuat dokumen markup keperluan pertukaran data antar sistem yang beraneka ragam. XML merupakan kelanjutan dari HTML (HyperText Markup Language) yang merupakan bahasa standar untuk melacak Internet.

Mengenal XML

XML didesain untuk mempu menyimpan data secara ringkas dan mudah diatur. Kata kunci utama XML adalah data (jamak dari datum) yang jika diolah bisa memberikan informasi

XML menyediakan suatu cara terstandarisasi namun bisa dimodifikasi untuk menggambarkan isi dari dokumen. Dengan sendirinya, XML dapat digunakan untuk menggambarkan sembarang view database, tetapi dengan suatu cara yang standar.

Tipe XML

XML memiliki tiga tipe file :

XML, merupakan standar format dari struktur berkas (file).

XSL, merupakan standar untuk memodifikasi data yang diimpor atau diekspor.

XSD, merupakan standar yang mendefinisikan struktur database dalam XML.

Keunggulan XML

Keunggulan XML bisa diringkas sebagai berikut :

Pintar (Intelligence). XML dapat menangani berbagai tingkat (level) kompleksitas.

Dapat beradaptasi. Dapat mengadaptasi untuk membuat bahasa sendiri. Seperti Microsoft membuaat bahasa MSXML atau Macromedia mengembangkan MXML.

Mudah pemeliharaannya.

Sederhana. XML lebih sederhana.

Mudah dipindah-pindahkan (Portability). XML mempunyai kemudahan perpindahan (portabilitas) yang lebih bagus.

Contoh dokumen XML sederhana

version="1.0" encoding="UTF-8"?>

nama="roti" waktu_persiapan="5 menit" waktu_masak="3 jam">

>Roti tawar>

jumlah="3" satuan="cangkir">Tepung>

jumlah="0,25" satuan="ons">Ragi>

jumlah="1,5" satuan="cangkir">Air hangat>

jumlah="1" satuan="sendok teh">Garam>

>

>Campur semua bahan dan uleni adonan sampai merata.>

>Tutup dengan kain lembab dan biarkan selama satu jam di ruangan yang hangat.>

>Ulangi lagi, letakkan di loyang dan panggang di oven.>

>Keluarkan, hidangkan>

>

>

Aturan Syntax XML

The syntax rules of XML are very simple and logical. The rules are easy to learn, and easy to use.


All XML Elements Must Have a Closing Tag

In HTML, elements do not have to have a closing tag:

This is a paragraph

This is another paragraph


In XML, it is illegal to omit the closing tag. All elements must have a closing tag:

This is a paragraph


This is another paragraph

Note: You might have noticed from the previous example that the XML declaration did not have a closing tag. This is not an error. The declaration is not a part of the XML document itself, and it has no closing tag.


XML Tags are Case Sensitive

XML tags are case sensitive. The tag is different from the tag .

Opening and closing tags must be written with the same case:

This is incorrect
This is correct

Note: "Opening and closing tags" are often referred to as "Start and end tags". Use whatever you prefer. It is exactly the same thing.


XML Elements Must be Properly Nested

In HTML, you might see improperly nested elements:

This text is bold and italic


In XML, all elements must be properly nested within each other:

This text is bold and italic


In the example above, "Properly nested" simply means that since the element is opened inside the element, it must be closed inside the element.


XML Documents Must Have a Root Element

XML documents must contain one element that is the parent of all other elements. This element is called the root element.



.....


XML Attribute Values Must be Quoted

XML elements can have attributes in name/value pairs just like in HTML.

In XML, the attribute values must always be quoted.

Study the two XML documents below. The first one is incorrect, the second is correct:


Tove
Jani



Tove
Jani





The error in the first document is that the date attribute in the note element is not quoted.


Entity References

Some characters have a special meaning in XML.

If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.

This will generate an XML error:

if salary < 1000 then

To avoid this error, replace the "<" character with an entity reference:

if salary < 1000 then

There are 5 predefined entity references in XML:

<

<

less than

>

>

greater than

&

&

ampersand

'

'

apostrophe

"

"

quotation mark

Note: Only the characters "<" and "&" are strictly illegal in XML. The greater than character is legal, but it is a good habit to replace it.


Comments in XML

The syntax for writing comments in XML is similar to that of HTML.


White-space is Preserved in XML

HTML truncates multiple white-space characters to one single white-space:

HTML:

Hello Tove

Output:

Hello Tove

With XML, the white-space in a document is not truncated.


XML Stores New Line as LF

In Windows applications, a new line is normally stored as a pair of characters: carriage return (CR) and line feed (LF). In Unix applications, a new line is normally stored as an LF character. Macintosh applications also use an LF to store a new line.

XML stores a new line as LF.

What is an XML Element?

An XML element is everything from (including) the element's start tag to (including) the element's end tag.

An element can contain:

other elements

text

attributes

or a mix of all of the above...



Harry Potter
J K. Rowling
2005
29.99


Learning XML
Erik T. Ray
2003
39.95


In the example above, and have element contents, because they contain other elements. also has an attribute (category="CHILDREN"). , <author>, <year>, and <price> have <span style="mso-bidi-font-weight: bold">text content</span> because they contain text.</span></p> <div class="MsoNoSpacing" style="text-align:center;line-height:200%" align="center"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> <hr align="center" size="2" width="100%"> </span></div> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">XML Naming Rules</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">XML elements must follow these naming rules:</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Names can contain letters, numbers, and other characters</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Names cannot start with a number or punctuation character</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Names cannot start with the letters xml (or XML, or Xml, etc)</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Names cannot contain spaces</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Any name can be used, no words are reserved.</span></p> <div class="MsoNoSpacing" style="text-align:center;line-height:200%" align="center"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> <hr align="center" size="2" width="100%"> </span></div> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Best Naming Practices</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Make names descriptive. Names with an underscore separator are nice: <first_name>, <last_name>.</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Names should be short and simple, like this: <book_title> not like this: <the_title_of_the_book>.</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Avoid "-" characters. If you name something "first-name," some software may think you want to subtract name from first.</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Avoid "." characters. If you name something "first.name," some software may think that "name" is a property of the object "first."</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Avoid ":" characters. Colons are reserved to be used for something called namespaces (more later).</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">XML documents often have a corresponding database. A good practice is to use the naming rules of your database for the elements in the XML documents.</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your software vendor doesn't support them.</span></p> <div class="MsoNoSpacing" style="text-align:center;line-height:200%" align="center"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> <hr align="center" size="2" width="100%"> </span></div> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">XML Elements are Extensible</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">XML elements can be extended to carry more information.</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Look at the following XML example:</span><span style="font-size:12.0pt;line-height: 200%;font-family:"Arial","sans-serif";mso-ansi-language:EN-US"></span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"; mso-ansi-language:EN-US"> </span></p> <table class="MsoTableGrid" style="border-collapse:collapse;border:none;mso-border-alt:solid windowtext .5pt; mso-yfti-tbllook:1184;mso-padding-alt:0in 5.4pt 0in 5.4pt" border="1" cellpadding="0" cellspacing="0"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="width:462.1pt;border:solid windowtext 1.0pt; mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt" valign="top" width="616"> <p class="MsoNoSpacing" style="line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><note> <br /> <to>Tove</to> <br /> <from>Jani</from> <br /> <body>Don't forget me this weekend!</body> <br /> </note></span><span style="font-size:12.0pt;line-height:200%; font-family:"Arial","sans-serif";mso-ansi-language:EN-US"></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"; mso-ansi-language:EN-US"> </span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <br /></td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Let's imagine that we created an application that extracted the <to>, <from>, and <body> elements from the XML document to produce this output:</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;background:yellow;mso-yfti-tbllook: 1184" border="1" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:.75pt .75pt .75pt .75pt"> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"; mso-bidi-font-weight:bold" lang="IN">MESSAGE</span><span style="font-size:12.0pt; line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> </span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"; mso-bidi-font-weight:bold" lang="IN">To:</span><span style="font-size:12.0pt; line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> Tove <br /> <span style="mso-bidi-font-weight:bold">From:</span> Jani</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Don't forget me this weekend!</span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Imagine that the author of the XML document added some extra information to it:</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><note> <br /> <date>2008-01-10</date> <br /> <to>Tove</to> <br /> <from>Jani</from> <br /> <heading>Reminder</heading> <br /> <body>Don't forget me this weekend!</body> <br /> </note></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Should the application break or crash?</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">No. The application should still be able to find the <to>, <from>, and <body> elements in the XML document and produce the same output.</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">One of the beauties of XML, is that it can be extended without breaking applications.</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> </span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">XML <span class="colorh1">Attributes</span></span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> </span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">XML elements can have attributes, just like HTML.</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Attributes provide additional information about an element.</span></p> <div class="MsoNoSpacing" style="text-align:center;line-height:200%" align="center"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> <hr align="center" size="2" width="100%"> </span></div> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">XML Attributes</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">In HTML, attributes provide additional information about elements:</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><img src="computer.gif"> <br /> <a href="demo.asp"></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">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:</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><file type="gif">computer.gif</file></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> </span></p> <div class="MsoNoSpacing" style="text-align:center;line-height:200%" align="center"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> <hr align="center" size="2" width="100%"> </span></div> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">XML Attributes Must be Quoted</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">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:</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><person sex="female"></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">or like this:</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><person sex='female'></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">If the attribute value itself contains double quotes you can use single quotes, like in this example:</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><gangster name='George "Shotgun" Ziegler'></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">or you can use character entities:</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><gangster name="George "Shotgun" Ziegler"></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> </span></p> <div class="MsoNoSpacing" style="text-align:center;line-height:200%" align="center"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> <hr align="center" size="2" width="100%"> </span></div> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">XML Elements vs. Attributes</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Take a look at these examples:</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><person sex="female"> <br /> <firstname>Anna</firstname> <br /> <lastname>Smith</lastname> <br /> </person></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="line-height:200%"><span style="font-size: 12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> </span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><person> <br /> <sex>female</sex> <br /> <firstname>Anna</firstname> <br /> <lastname>Smith</lastname> <br /> </person></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">In the first example sex is an attribute. In the last, sex is an element. Both examples provide the same information.</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">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.</span></p> <div class="MsoNoSpacing" style="text-align:center;line-height:200%" align="center"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> <hr align="center" size="2" width="100%"> </span></div> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">My Favorite Way</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">The following three XML documents contain exactly the same information:</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">A date attribute is used in the first example:</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><note date="10/01/2008"> <br /> <to>Tove</to> <br /> <from>Jani</from> <br /> <heading>Reminder</heading> <br /> <body>Don't forget me this weekend!</body> <br /> </note></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="line-height:200%"><span style="font-size: 12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">A date element is used in the second example:</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><note> <br /> <date>10/01/2008</date> <br /> <to>Tove</to> <br /> <from>Jani</from> <br /> <heading>Reminder</heading> <br /> <body>Don't forget me this weekend!</body> <br /> </note></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="line-height:200%"><span style="font-size: 12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">An expanded date element is used in the third: (THIS IS MY FAVORITE):</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><note> <br /> <date> <br /> <day>10</day> <br /> <month>01</month> <br /> <year>2008</year> <br /> </date> <br /> <to>Tove</to> <br /> <from>Jani</from> <br /> <heading>Reminder</heading> <br /> <body>Don't forget me this weekend!</body> <br /> </note></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> </span></p> <div class="MsoNoSpacing" style="text-align:center;line-height:200%" align="center"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> <hr align="center" size="2" width="100%"> </span></div> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Avoid XML Attributes?</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Some of the problems with using attributes are:</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">attributes cannot contain multiple values (elements can)</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">attributes cannot contain tree structures (elements can)</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">attributes are not easily expandable (for future changes)</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data.</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Don't end up like this:</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><note day="10" month="01" year="2008" <br /> to="Tove" from="Jani" heading="Reminder" <br /> body="Don't forget me this weekend!"> <br /> </note></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> </span></p> <div class="MsoNoSpacing" style="text-align:center;line-height:200%" align="center"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> <hr align="center" size="2" width="100%"> </span></div> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">XML Attributes for Metadata</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">Sometimes ID references are assigned to elements. These IDs can be used to identify XML elements in much the same way as the id attribute in HTML. This example demonstrates this:</span></p> <table class="MsoNormalTable" style="width:100.0%;mso-cellspacing:0in;mso-yfti-tbllook:1184;mso-padding-alt: 0in 0in 0in 0in" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes"> <td style="padding:0in 0in 0in 0in"> <p class="MsoNoSpacing" style="line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"><messages> <br /> <note id="501"> <br /> <to>Tove</to> <br /> <from>Jani</from> <br /> <heading>Reminder</heading> <br /> <body>Don't forget me this weekend!</body> <br /> </note> <br /> <note id="502"> <br /> <to>Jani</to> <br /> <from>Tove</from> <br /> <heading>Re: Reminder</heading> <br /> <body>I will not</body> <br /> </note> <br /> </messages></span></p> </td> </tr> </tbody></table> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">The id attributes above are for identifying the different notes. It is not a part of the note itself.</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN">What I'm trying to say here is that metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.</span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"" lang="IN"> </span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"; mso-ansi-language:EN-US"> </span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"; mso-ansi-language:EN-US"> </span></p> <p class="MsoNoSpacing" style="text-align:justify;line-height:200%"><span style="font-size:12.0pt;line-height:200%;font-family:"Arial","sans-serif"; mso-ansi-language:EN-US"> </span></p>

No comments:

Post a Comment