You are on page 1of 22

!"!

$%&'()% *
Advanced XML 1
Explain DTD
Create DTD:
Declaring Elements
Declaring Attributes
Declaring Entities
!"#$%&'$(
Advanced XML 2
A feature of SGML, inherited by XML.
Contains the list of tags that specifies the
grammatical structure of an XML document.
DTD defines the way elements relate to one another
within the documents tree structure, and specifies
the attributes.
DTD are of two types:
An external DTD
An internal DTD
*+%,-$./ 012$ *$3.4&+. 5*0*6
Advanced XML 3
Provide an application independent way of sharing
data.
Interchange data between independent groups of
people.
Can be used by the application to verify that valid data
has been entered.
Defines the legal building blocks of an XML
document.
781 ,($ *0*
Advanced XML 4
<!DOCTYPE dtd-name
[
<!ELEMENT element-
name (element-content
type) >
<!ATTLIST element-
name attribute-name
attribute-type
default-value>
]>
DOCTYPE declaration
ELEMENT declaration
ATTRIBUTE
declaration
9/:,%/,:$ +; < *0*
Advanced XML 5
=./$:.<> *0*

<!DOCTYPE SHOWROOM [
<!ELEMENT SHOWROOM (TV|LAPTOP)+>
<!ELEMENT TV (#PCDATA)>
<!ATTLIST TV count CDATA #REQUIRED>
<!ELEMENT LAPTOP (#PCDATA)>
<!ATTLIST LAPTOP count CDATA REQUIRED>
]>
Written directly in the
XML document after the
XML declaration.
Writing the DTD within
the DOCTYPE definition
is called as Wrapping.
The file with the DTD
and XML code has a .xml
extension.

Advanced XML 6
?@/$:.<> *0*
The DTD reference in the XML document file:
<!DOCTYPE SHOWROOM SYSTEM "show.dtd">

The show.dtd file:
<!ELEMENT SHOWROOM (TV|LAPTOP)+>
<!ELEMENT TV (#PCDATA)>
<!ATTLIST TV count CDATA #REQUIRED>
<!ELEMENT LAPTOP (#PCDATA)>
<!ATTLIST LAPTOP count CDATA #REQUIRED>

Exists outside the
content of a
document.
The DTD file has
a .dtd extension.
Reference to the
DTD file is added at
the beginning of the
XML file.

Advanced XML 7
XML elements are declared with an element declaration.
Syntax
<!ELEMENT element-name(element-content type)>
Example
<!ELEMENT SHOWROOM (TV|LAPTOP)+>
*$%><:4.A <. ?>$-$./
Advanced XML 8
EMPTY element content type specifies that the
element has no child elements or character data.
Syntax
<!ELEMENT element-name EMPTY>
Example
<!ELEMENT img EMPTY>
Empty elements with attributes are possible:
<img src=Tittle.gif />

?-2/1 ?>$-$./
Advanced XML 9
Syntax
<!ELEMENT element-name (#PCDATA)>
or
<!ELEMENT element-name (ANY)>

Where:
#PCDATA = element contains character data that is to be parsed
ANY = element with any content

?>$-$./ B4/8 *</<
Advanced XML 10
Elements with one or more children are defined with the name
of the child element inside the parentheses.
Syntax
<!ELEMENT element-name (child-element-name)>
Example
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
?>$-$./ B4/8 C84>D ?>$-$./(
Advanced XML 11
Element occurrences
Only one occurrence
<!ELEMENT element-name (child-name)>
Minimum one
occurrence
<!ELEMENT
element-name
(child-name+)>
Zero or more
occurrences
<!ELEMENT
element-name
(child-
name*)>
Zero or one occurrence
<!ELEMENT element-name (child-name?)>
!%%,::$.%$(
Advanced XML 12
<!-- Element A consists of a single element B. -->
<!ELEMENT A (B)>

<!-- Element A consists of element B followed by
element C. -->
<!ELEMENT A (B, C)>

<!-- Element A consists of a sequence, including a
choice subgroup. -->
<!ELEMENT A (B, (C | D), E>
9$E,$.%$
Advanced XML 13
<!-- Element A consists of either element B or element
C. -->
<!ELEMENT A (B | C)>

<!-- Element A consists of a choice, including a
sequence subgroup. -->
<!ELEMENT A (B | C | (D, E))>

C8+4%$
Advanced XML 14
Elements can have attributes.
Syntax
<!ATTLIST element-name attribute-name attribute-type default-
value>
Example
<!DOCTYPE Book [
<!ELEMENT Book (Title, Chapter+)>
<!ATTLIST Book Author CDATA #REQUIRED>
<!ELEMENT Chapter (#PCDATA)>
<!ATTLIST Chapter id (4 | 7) #REQUIRED>
<!ELEMENT Title (#PCDATA)>
]>
*$%><:4.A FG:4",/$
Advanced XML 15


Value



Explanation




CDATA




The value is character data


(eval|eval|..)



The value must be an enumerated value


ID



The value is an unique id


IDREF



The value is the id of another element


IDREFS



The value is a list of other ids


NMTOKEN



The value is a valid XML name


NMTOKENS



The value is a list of valid XML names


ENTITY



The value is an entity


ENTITIES



The value is a list of entities


NOTATION



The value is a name of a notation


xml:



The value is predefined

FG:4",/$ 5H<>,$ 012$6
Advanced XML 16


Value



Explanation


#DEFAULT



The attribute has a default value.


#REQUIRED



The attribute value must be included in the
element.


#IMPLIED



The attribute does not have to be included.


#FIXED



The attribute value is fixed.

FG:4",/$ 5*$;<,>/ H<>,$6
Advanced XML 17
Entities that have their contents within the XML
document are called internal entities.
Syntax
<!ENTITY entity-name "entity-value">
Example
<!ENTITY writer Nicole D.">
<!ENTITY copyright "Copyright FPT.">

<author>&writer;&copyright;</author>
=./$:.<> ?.&/1 *$%><:<&+.
Advanced XML 18
Entities whose contents are found outside the
XML document are called external entities.
They are declared using the SYSTEM keyword.
Syntax
<!ENTITY entity-name SYSTEM "URI/URL">
Example

<!ENTITY writer SYSTEM "http://www.xml101.com/
entities/entities.xml">

<!ENTITY copyright SYSTEM "http://www.xml101.com/
entities/entities.dtd">
?@/$:.<> ?.&/1 *$%><:<&+.
Advanced XML 19
A well-formed document is one that conforms to the basic
rules of XML.
A valid document is well formed and is also validated against
a DTD.
The DTD specifies the grammatical structure of an XML
document, by allowing XML parsers to understand and
interpret the documents contents.
The use of the SYSTEM keyword indicates to the parser that
this is an external declaration, and that the set of rules for this
XML document can be found in a specified file.
EMPTY element content type specifies that the element has
no child elements or character data.
9,--<:1 I J
Advanced XML 20
#CDATA means that the element contains character data that is not
to be parsed by a parser.
#PCDATA means that the element contains data that is to be parsed
by a parser.
Specifying a default value for an attribute in the DTD ensures that
the attribute will get a value, even if the author of the XML
document does not include it.
Specifying the value of an attribute as Implied means that the
particular attribute is not mandatory and can be specified in the
XML document.
Specifying the value of an attribute as Required means that the
particular attribute is mandatory (that is, its value must be provided
in the XML document).
9,--<:1 I K
Advanced XML 21
ID is the identifier type, and should be unique. This
attribute value is used to search for a particular instance of
an element. Each element can only have one attribute of type
ID.
A DTD can be either External or Internal.
Entities allow us to create an alias to some large piece of
text, so that, in the document, the same piece of text can be
referred to, simply by referring to the alias.
Namespaces allow us to combine documents from different
sources, and be able to identify which elements or attributes
come from which source.
9,--<:1 I L
Advanced XML 22

You might also like