Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
Document Type Declaration
Declaring a DTD
Document Type Declaration
– Introduce DTDs into XML documents
– Placed in XML document’s prolog
A document type definition is a collection of – Begins with ‘<!DOCTYPE’ and ends with ‘>’
rules or declarations that define the content and – Often referred to as the DOCTYPE declaration
structure of the document. – Can point to
• External subsets
A document type declaration attaches those – Declarations outside document
rules to the document’s content. – Exist in different file
– typically ending with .dtd extension
• Internal subsets
– Declarations inside document
– Visible only within document in which it resides
2
Starting the Document Type Definition:
Declaring Document Elements Declaring Document Elements
The element name is case sensitive.
Every element used in the document must be declared in the DTD DTDs define five different types of element content:
for the document to be valid. – Any elements. No restrictions on the element’s content.
• <!ELEMENT element ANY>
An element type declaration specifies the name of the element
– Empty elements. The element cannot store any content.
and indicates what kind of content the element can contain.
• <!ELEMENT element EMPTY>
– Character data. The element can only contain a text string.
The element declaration syntax is: • <!ELEMENT element (#PCDATA)>
• The keyword #PCDATA stands for “parsed-character data” and is any well-
<!ELEMENT element content-model> formed text string.
– Elements. The element can only contain child elements.
• <!ELEMENT element (child elements)>
Where element is the element name and content-model specifies
what type of content the element contains. – Mixed. The element can contain both character data and child
elements.
• <!ELEMENT element (#PCDATA|child1|child2|…)*>
• The parent element can contain character data or any number of the
specified child elements, or it can contain no content at all.
Sequences
Types of Element Content
Sequences
– Specify order in which elements occur
The declaration – Comma (,) is used as delimiter
<!ELEMENT element (child1, child2, …)>
<!ELEMENT customer (phone)>
<!ELEMENT phone (#PCDATA)> <classroom>
indicates the Customer element can only have one child, named <teacher>1</teacher>
Phone. You cannot repeat the same child element more than once <student>20</student>
with this declaration. The phone element can only contain character </classroom>
data.
Defining the classroom element in a DTD:
Valid XML markup could be:
<customer> <!ELEMENT classroom (teacher, student)>
<phone>02 3333 4444</phone>
</customer> The order of the child elements in the XML file must match the
order defined in the element declaration
3
Sequences Occurrence Indicators
(Modifying Symbols)
4
Declaring an external DTD DTD – External Subset
Book.xml:
The real power of XML comes from an external DTD that can be shared <?xml version="1.0"?>
among many documents written by different authors.
<!DOCTYPE book SYSTEM “book.dtd”>
Each XML document can only be linked to one external DTD <book>
<title>Web Applications</title>
The DOCTYPE declaration for an external subset is: <author>John Doe</author>
<chapters>
<!DOCTYPE root SYSTEM “URL”> Or <chapter>Introduction</chapter>
<chapter>ASP</chapter>
<!DOCTYPE root SYSTEM “URL” <chapter>XML</chapter>
[ </chapters>
declarations </book>
]> Book.dtd:
Where root is the name of the document’s root element, URL is the location <!ELEMENT book(title,author,chapters)>
and name of the external dtd file, and declarations are the statements that <!ELEMENT title(#PCDATA)>
comprise the DTD. <!ELEMENT author(#PCDATA)>
<!ELEMENT chapters(chapter+)>
<!ELEMENT chapter(#PCDATA)>
5
Attribute Declarations Declaring Element Attributes
For a document to be valid, all the attributes associated The syntax to declare a list of attributes is:
with elements must also be declared.
<!ATTLIST element attribute1 type1 default1
– You must add an attribute-list declaration to the document’s
attribute2 type2 default2
DTD.
attribute3 type3 default3…>
Attribute Declaration:
– Specifies all the attributes an element has – element is the name of the element associated with
the attributes,
– Uses ATTLIST attribute list declaration
– attribute is the name of an attribute,
• Lists the names of all attributes associated with a specific element
• Specifies the data type of the attribute
– type is the attribute’s data type, and
• Indicates whether the attribute is required or optional – default indicates whether the attribute is required or
• Provides a default value for the attribute, if necessary
implied, and whether it has a fixed or default value.
6
1 <?xml version = "1.0"?>
Attribute Tokens 2
5
This figure shows the seven attribute tokens
6 <!DOCTYPE bookstore [
7 <!ELEMENT bookstore ( shipping+, book+ )> Each shipping element has a unique identifier
(shipID)
8 <!ELEMENT shipping ( duration )>
15 <bookstore>
17 <duration>2 to 4 days</duration>
18 </shipping>
19
7
Attribute Declarations (fig. 3.13)
Entities
Entities are storage units for a document’s
content.
The most fundamental entity is the XML
document itself and is known as the document
entity.
Entities can also refer to:
– a text string
– a DTD
– an element or attribute declaration
– an external file containing character or binary data
8
General Parsed Entities General External Entities
General entities are declared in the DTD of a document. The syntax is:
<!ENTITY entity “value”>
Where entity is the name assigned to the entity and value is the general General entities can refer to values located in
entity’s value. external files. The syntax is:
For example, an entity named “Pixal” can be created to store a company's <!ENTITY entity SYSTEM “URL”>
official name:
<!ENTITY Pixal “Pixal Digital Products”> For example, in the declaration:
<!ENTITY headlines SYSTEM
After an entity is declared, it can be referenced anywhere within the http://www.newsflash.com/stories.xml>
document.
<Title>This is the home page of &Pixal;</Title> An entity named “headlines” gets its value from
This is interpreted as the document stories.xml, located at
<Title>This is the home page of Pixal Digital Products</Title> http://www.newsflash.com/stories.xml
9
Using Parameter Entities to Combine Multiple
DTDs
Readings
DWAX 2010.1 39
10