2
XML overview
XML stands for Extensible Markup Language. XML defines a set of rules for encoding documents in a format which is both human-readable and machine-readable. The XML is a subset of Standard Generalized Markup Language (SGML).
3
Usage of XML
XML Separates Data from HTML
XML Simplifies Data Sharing
XML Simplifies Data Transport
XML Simplifies Platform Changes
XML Makes Data More Available
Internet Languages Written in XML (XHTML, XML, SVG, RSS)
4
Some methods of describing data between information systems
Text files (parsable, non-standard, incompatible structured data format, human readable)
HTML (usually static data & presentation layer, human readable)
XML (parsable, standard, compatible, extensible, human readable)
Database files (standard & incompatible format between systems, human unreadable)
5
Some applications of XML
Microsoft Office (Office Open XML).
OpenOffice.org and LibreOffice (OpenDocument).
Apple's iWork. XML base language for communication protocols.
Microsoft .NET Framework use XML files for configuration.
Apple has an implementation of a registry based on XML.
Transfer data over the Internet.
6
Building Blocks of XML Documents
Elements (main building blocks)
Attributes (extra information about elements)
Entities (special characters like the less than sign (<) “<”)
PCDATA (character data as the text found between the start tag and the end tag)
CDATA (Tags inside the text will NOT be treated as markup and entities)
7
XML component formatting
Markup
A markup construct that begins with < and ends with >.
XML declaration
XML documents begin by declaring some information
Example:
<?xml version="1.0" encoding="UTF-8"?>
8
XML component formatting
Tag
Tags come in three formats:
start-tags; <section>
end-tags; </section>
empty-element tags; <line-break />
9
XML component formatting
Element
A logical document component which either begins with a start-tag and ends with a matching end-tag or consists only of an empty-element tag. The characters between the start- and end-tags, if any, are the element's content, and may contain markup, including other elements, which are called child elements.
Example:
<Message>Hello, world.</Message >
10
XML component formatting
Attribute
A markup construct consisting of a name/value that exists within a start-tag or empty-element tag.
Example:
<Dosage AgeCategory="Child">2</ Dosage >
11
XML component formatting
Escaping
XML provides escape facilities for including characters which are problematic to include directly.
Example:
< represents "<"
> represents ">"
& represents "&"
' represents '
" represents "
12
XML component formatting
Comments
Comments start with "<!--" and end with "-->". For compatibility with SGML, the string "--" (double-hyphen) is not allowed inside comments, comments cannot be nested.
Example:
<!-- Comments -->
13
Elements vs Attributes
<Medicines>
<Medicine>
<Medicine_Code>C_100</Medicine_Code>
<Name>Cital</Name>
<Type>Capsule</Type>
<Side_Effects>None</Side_Effects>
<doze />
</Medicine>
</Medicines>
14
Elements vs Attributes
<Medicines>
<Medicine Medicine_Code="C_100">
<Name>Cital</Name>
<Type>Capsule</Type>
<Side_Effects>None</Side_Effects>
<doze />
</Medicine>
</Medicines>
15
DTD Overview
XML Document Type Declaration commonly known as DTD is a way to describe precisely the XML language. DTDs check the validity of, structure.
DTD describes:
The elements that can appear in an XML document.
The order in which they can appear.
Optional and mandatory elements.
Element attributes and whether they are optional or mandatory.
Whether attributes can have default values.
16
Associating DTDs with documents
DTDs sorts of declaration:
External subset
Internal subset
When a DTD is declared within the file it is called Internal DTD and if it is declared in a separate file it is called External DTD
17
Example (internal subset)<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
18
Example (external subset)
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
19
Example (external subset)
"note.dtd", which contains the DTD:
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
20
Declaring Elements
In a DTD, XML elements are declared with the following syntax:
<!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>
21
Declaring Elements
Empty Elements
Empty elements are declared with the category keyword EMPTY:
DTD:
<!ELEMENT br EMPTY>
XML:
<br />
22
Declaring Elements
Elements with Parsed Character Data
<!ELEMENT from (#PCDATA)>
Elements with any Contents
<!ELEMENT note ANY>
Elements with Children
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
23
Declaring Elements
Declaring Only One Occurrence of an Element
<!ELEMENT note (message)>
Declaring Minimum One Occurrence of an Element
<!ELEMENT note (message+)>
Declaring Zero or More Occurrences of an Element
<!ELEMENT note (message*)>
Declaring Zero or One Occurrences of an Element
<!ELEMENT note (message?)>
24
Declaring Elements
Declaring either/or Content
<!ELEMENT note (to,from,header,(message|body))>
Declaring Mixed Content
<!ELEMENT note (#PCDATA|to|from|header|message)*>
25
Declaring Attributes
Declaring Attributes
DTD example:
<!ATTLIST payment type CDATA "check">
XML example:
<payment type="check" />
26
Declaring Attributes
Some of the attribute-types:
1. CDATA The value is character data
2. (en1|en2|..) The value must be one from an enumerated list
Some of the attribute-values:
1. value The default value of the attribute
2. #REQUIRED The attribute is required
3. #IMPLIED The attribute is optional
4. #FIXED value The attribute value is fixed
27
Declaring Attributes
A Default Attribute Value
DTD:
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
Valid XML:
<square width="100" />
28
Declaring Attributes
#REQUIRED
DTD:
<!ATTLIST person number CDATA #REQUIRED>
Valid XML:
<person number="5677" />
Invalid XML:
<person />
29
Declaring Attributes
#IMPLIED
DTD:
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax="555-667788" />
Valid XML:
<contact />
30
Declaring Attributes
#FIXED
DTD:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
Valid XML:
<sender company="Microsoft" />
Invalid XML:
<sender company="W3Schools" />
31
Declaring Attributes
Enumerated Attribute Values
DTD:
<!ATTLIST payment type (check|cash) "cash">
XML example:
<payment type="check" />
or
<payment type="cash" />
32
Declaring Attributes
Internal Entity Declaration
DTD Example:
<!ENTITY writer "Donald Duck.">
<!ENTITY copyright "Copyright W3Schools.">
XML example:
<author>&writer;©right;</author>
33
Declaring Attributes
External Entity Declaration
DTD Example:
<!ENTITY writer SYSTEM "http://www.w3schools.com/entities.dtd">
<!ENTITY copyright SYSTEM "http://www.w3schools.com/entities.dtd">
XML example:
<author>&writer;©right;</author>
34
DTD benefits & Limitations
Benefits of using DTD
1. Documentation as a user/developer can understand the structure of the data.
2. Validation - check the validity of XML.
Limitations of using DTD
1. It does not support the namespaces. Namespace is a mechanism by which element and attribute names can be assigned to groups. However, in a DTD namespaces have to be defined within the DTD, which violates the purpose of using namespaces.
2. It supports only the text string data type.
3. The concept of inheritance cannot be applied on the DTDs.
Top Related