Pages

Wednesday, May 23, 2012

XML DTD and Schema


XML DTD
The purpose of a DTD is to define the structure of an XML document.
 It defines the structure with a list of legal elements:

<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>

XML Schema
XML-based alternative to DTD, called XML Schema:
<xs:element name="note">

<xs:complexType>
  <xs:sequence>
    <xs:element name="to" type="xs:string"/>
    <xs:element name="from" type="xs:string"/>
    <xs:element name="heading" type="xs:string"/>
    <xs:element name="body" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

</xs:element>

XML Validation


XML Validation
  • XML with correct syntax is "Well Formed" XML.
  • XML validated against a DTD is "Valid" XML.
A "Well Formed" XML document has correct XML syntax.
The syntax rules were described in the previous chapters:
  • XML documents must have a root element
  • XML elements must have a closing tag
  • XML tags are case sensitive
  • XML elements must be properly nested
  • XML attribute values must be quoted
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tom</to>
<from>Jerry</from>
<heading>Reminder</heading>
<body>Don't forget Birthday Party this weekend!</body>
</note>

Valid XML Documents
A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a Document Type Definition (DTD):

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE note SYSTEM "Note.dtd">

<note>
<to>Tom</to>
<from>Jerry</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

The DOCTYPE declaration in the example above, is a reference to an external DTD file.



Tuesday, May 22, 2012

XML Attributes


Why to Avoid XML Attributes?
Some of the problems with using attributes are:
  • attributes cannot contain multiple values (elements can)
  • attributes cannot contain tree structures (elements can)
  • attributes are not easily expandable (for future changes)
  1. Attributes are difficult to read and maintain.
  2.  Use elements for data.
  3.  Use attributes for information that is not relevant to the data.


Don't end up like this:

<note day="10" month="01" year="2008"
to="Tom" from="Jerry" heading="Reminder"
body="Don't forget Birthday Party this weekend!">
</note>

XML Attributes for Metadata
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:

<messages>

  <note id="201">
    <to>Tom</to>
    <from>Jerry</from>
    <heading>Reminder</heading>
    <body>Don't forget Birthday Party this weekend!</body>
  </note>

  <note id="202">
    <to>Jerry</to>
    <from>Tom</from>
    <heading>Re: Reminder</heading>
    <body>I will not be available to make it to Birthday Party</body>
  </note>

</messages>

  • The id attributes above are for identifying the different notes
  •  It is not a part of the note itself.
  • 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.

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).

Benefits of XML



XML Simplifies Data Sharing :-


  •  In the real world, computer systems and databases contain data in incompatible formats.
  • XML data is stored in plain text format. 
  • This provides a software- and hardware-independent way of storing data.
  • This makes it much easier to create data that can be shared by different applications.




XML Simplifies Data Transport :-


  • One of the most time-consuming challenges for developers is to exchange data between incompatible systems over the Internet.
  • Exchanging data as XML greatly reduces this complexity, since the data can be read by different incompatible applications.




XML Simplifies Platform Changes :-


  • Upgrading to new systems (hardware or software platforms), is always time consuming. Large amounts of data must be converted and incompatible data is often lost.
  • XML data is stored in text format. This makes it easier to expand or upgrade to new operating systems, new applications, or new browsers, without losing data.




XML Makes Your Data More Available:-


  • Different applications can access your data, not only in HTML pages, but also from XML data sources.
  • With XML, your data can be available to all kinds of "reading machines" (Handheld computers, voice machines, news feeds, etc), and make it more available for blind people, or people with other disabilities.
  • XML is Used to Create New Internet Languages
  • A lot of new Internet languages are created with XML.

Tuesday, May 15, 2012

XML- Introduction



XML stands for eXtensible Markup Language.
XML is designed to transport and store data.
XML is important to know, and very easy to learn.
XML is mostly used on the websites where data changes frequently but the format of data storage and display is the same.
For eg : Yahoo ! Cricket, Stock Market Websites are few to name.
Here Feeding the Information on regular basis will be a tedious work rather using XML to do the same work will save time.


XML Document Example


<?xml version="1.0"?>
<note>
    <to>Ram</to>
    <from>Peter</from>
    <heading>Reminder</heading>
    <body>Don't forget birthday party this weekend!</body>
</note>




  • XML was designed to transport and store data.
  • HTML was designed to display data.


What You Should Already Know
Before you continue you should have a basic understanding of the following:


  • HTML
  • JavaScript
How Can XML be Used?
  • It is used in many aspects of web development, often to simplify data storage and sharing.
  • It Separates Data from HTML
  • If you need to display dynamic data in your HTML document, it will take a lot of work to edit the HTML each time the data changes.(as mentioned at the start.)
  • With XML, data can be stored in separate XML files.
  •  This way you can concentrate on using HTML for layout and display, and be sure that changes in the underlying data will not require any changes to the HTML.
  • With a few lines of JavaScript code, you can read an external XML file and update the data content of your web page.