XML notes
XML notes
•[
• first declaration
• second declaration . .
Document Type Definition (DTD)
• <?xml version="1.0"?> • <address>
• <!DOCTYPE address [ • <name>
• <!ELEMENT address (name, email, phone, • <first>Rohit</first>
birthday)>
• <last>Sharma</last>
• <!ELEMENT name (first, last)>
• </name>
• <!ELEMENT first (#PCDATA)>
• <email>sharmarohit@gmail.com</email>
• <!ELEMENT last (#PCDATA)>
• <phone>9876543210</phone>
• <!ELEMENT email (#PCDATA)>
• <birthday>
• <!ELEMENT phone (#PCDATA)>
• <year>1987</year>
• <!ELEMENT birthday (year, month, day)>
• <month>June</month>
• <!ELEMENT year (#PCDATA)>
• <day>23</day>
• <!ELEMENT month (#PCDATA)>
• </birthday>
• <!ELEMENT day (#PCDATA)>
• </address>
• ]>
• The DTD above is interpreted like this: • !ELEMENT email defines the email element to
be of type “#PCDATA”.
• !DOCTYPE address defines that the root
element of this document is address. • !ELEMENT phone defines the phone element
to be of type “#PCDATA”.
• !ELEMENT address defines that the address
element must contain four elements: “name, • !ELEMENT birthday defines that the birthday
email, phone, birthday”. element must contain three elements “year,
month, day”.
• !ELEMENT name defines that the name
element must contain two elements: “first, • !ELEMENT year defines the year element to be of
type “#PCDATA”.
last”.
• !ELEMENT month defines the month element to be of
• !ELEMENT first defines the first element to be of type “#PCDATA”.
type “#PCDATA”.
• !ELEMENT day defines the day element to be of type
• !ELEMENT last defines the last element to be of “#PCDATA”.
type “#PCDATA”.
•
Example
Example
XML Namespaces
• XML Namespace is used to avoid element name conflict in XML
document.
• XML Namespace Declaration
• An XML namespace is declared using the reserved XML attribute.
This attribute name must be started with "xmlns".
• <element xmlns:name = "URL">
• Here, namespace starts with keyword "xmlns". The word name is a
namespace prefix.
• The URL is a namespace identifier. A Uniform Resource Identifier
(URI) is a string of characters which identifies an Internet Resource.
Let's take an example with two tables:
• This XML carries information about an HTML table, and a piece of furniture:
If these XML fragments were added together, there would be a
<table> name conflict. Both contain a <table> element, but the elements
have different content and meaning.
<tr>
<td>Apples</td>
<table>
<td>Bananas</td>
<name>African Coffee
</tr>
Table</name>
</table>
<width>80</width>
<length>120</length>
</table>
Let's take an example with two tables:
• This XML carries information about an HTML table, and a piece of furniture:
Solving the Name Conflict Using a Prefix
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
The xmlns Attribute
• When using prefixes in XML, a namespace for the prefix must be defined.
• The namespace can be defined by an xmlns attribute in the start tag of an element.
<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="https://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
XML Schema
•An XML Schema describes the structure of an XML
document, just like a DTD.