Sunday 24 November 2013

XML & DTD



Document Type Definition


The purpose of a DTD (Document Type Definition) is to define the legal building blocks of an XML document. A DTD defines the document structure with a list of legal elements and attributes. A DTD can be declared inline inside an XML document, or as an external reference.
Internal DTD Declaration:
If the DTD is declared inside the XML file, it should be wrapped in a DOCTYPE definition with the following syntax:
<! DOCTYPE root-element [element-declarations]>
XML document with an internal DTD:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weather[
<!ELEMENT weather (day1)>
<!ELEMENT day1 (location,date,temp)>
<!ELEMENT location (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT temp (#PCDATA)>
<!ENTITY GM "Good Morning">
<!ENTITY GN "Good Night">
]>

<weather>
<day1 >
<location>&GM; Sanfransico </location>
<date>20/04/2013</date>
<temp>20'c</temp>
</day1>
</weather>

The DTD above is interpreted like this:

!DOCTYPE note defines that the root element of this document is note
!ELEMENT note defines that the note element contains four elements: "to, from, heading, body" 
!ELEMENT to defines the to element to be of type "#PCDATA"
!ELEMENT from defines the from element to be of type "#PCDATA"
!ELEMENT heading defines the heading element to be of type "#PCDATA"
!ELEMENT body defines the body element to be of type "#PCDATA"

External DTD Declaration
If the DTD is declared in an external file, it should be wrapped in a DOCTYPE definition with the syntax:
<! DOCTYPE root-element SYSTEM "filename">
whether.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weather SYSTEM "weather.dtd">
<weather>
<day1>
<location>&GM;Vita</location>
<date>20/04/2013</date>
<temp>20'c</temp>
</day1>
</weather>

weather.dtd
<!ELEMENT weather (day1)>
<!ELEMENT day1 (location,date,temp)>
<!ELEMENT location (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT temp (#PCDATA)>
<!ENTITY GM "Good Morning">
<!ENTITY GN "Good Night">

Why to Use a DTD?
With a DTD, each of your XML files can carry a description of its own format. With a DTD, independent groups of people can agree to use a standard DTD for interchanging data. Your application can use a standard DTD to verify that the data you receive from the outside world is valid. You can also use a DTD to verify your own data.
The Building Blocks of XML Documents
All XML documents are made up by the following building blocks:
Elements, Attributes, Entities, PCDATA, and CDATA .

Elements
Elements are the main building blocks of both XML and HTML documents.

Attributes
Attributes provide extra information about elements. Attributes are always placed inside the opening tag of an element. Attributes always come in name/value pairs.
Entities .Some characters have a special meaning in XML, like the less than sign (<) that defines the start of an XML tag.

PCDATA
PCDATA means parsed character data.

CDATA
CDATA means character data.
 

XML & RELATED TECHNIQUES



What is XML?
XML stands for Extensible Markup Language which is much like HTML. XML was designed to carry data, not to display data.
XML tags are not predefined. You must define your own tags. XML is designed to be self-descriptive.

Features of XML:
1.   With XML You Invent Your Own Tags
2.   XML Separates Data from HTML
3.   XML Simplifies Data Sharing
4.   XML Simplifies Data Transport

XML Syntax Rules:
All XML Elements Must Have a Closing Tag.
XML Tags are Case Sensitive.
XML Elements Must be Properly Nested.
XML Documents Must Have a Root Element.
XML Attribute Values Must be quoted.

Comments in XML:
 The syntax for writing comments in XML is similar to that of HTML.
<! -- This is a comment -->


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, simple text or a mixture of both. Elements can also have attributes.
E.g. XML Example
<?xml version="1.0" encoding="UTF-8"?>
<weather>
<day1>
<location>Sanfransico</location>
<date>20/04/2013</date>
<temp>20'c</temp>
</day1>
</weather>

In the example above, <weather> and <day1>  have element contents, because they contain other elements. <location>  has text content because it contains text.

XML Naming Rules
XML elements must follow these naming rules:
Names can contain letters, numbers, and other characters
Names cannot start with a number or punctuation character
Names cannot start with the letters xml (or XML, or Xml, etc)
Names cannot contain spaces
Any name can be used, no words are reserved.

Well Formed XML Documents:
A "Well Formed" XML document has a correct XML syntax.
XML Namespaces
XML Namespaces provide a method to avoid element name conflicts


Name Conflicts
In XML, element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications.
XML Namespaces - The xmlns Attribute
When using prefixes in XML, a so-called namespace for the prefix must be defined. The namespace is defined by the xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns: prefix="URI".

Uniform Resource Identifier (URI)
A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource. The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Universal Resource Name (URN).

ShareThis

Related Posts Plugin for WordPress, Blogger...